美文网首页
2018-02-13 Python 语法糖: 装饰器的使用

2018-02-13 Python 语法糖: 装饰器的使用

作者: 罗兆峰 | 来源:发表于2018-02-13 15:26 被阅读0次

class A(object):

    def  __call__(self, *args, **kw):

        self.run(*args,**kw)

    def run(self,*args,**kw):

        raise NotImplementedError

def decorator(fun):

    class B(A):

        def  run(self,*args,**kw):

            return fun(*args,**kw)

    retrun B()

@decorator

def test(string):

    return string

test('A')

编译器处理过程:

先将 text 重新赋值:

test = decorator(test)

然后开始调用过程

test('A') = decorator(test)('A')

相关文章

网友评论

      本文标题:2018-02-13 Python 语法糖: 装饰器的使用

      本文链接:https://www.haomeiwen.com/subject/hqijtftx.html