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

2018-02-13  本文已影响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')

上一篇 下一篇

猜你喜欢

热点阅读