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')
編譯器處理過(guò)程:
先將 text 重新賦值:
test = decorator(test)
然后開(kāi)始調(diào)用過(guò)程
test('A') = decorator(test)('A')