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')