class myDecorator(object):
def __init__(self, f):
print "inside myDecorator.__init__()"
f() # Prove that function definition has completed
def __call__(self):
print "inside myDecorator.__call__()"
@myDecorator
def aFunction():
print "inside aFunction()"
def main():
print "main starts....."
aFunction()
print "main ends....."
输出 :
inside myDecorator.__init__()
inside aFunction()
main starts.....
inside myDecorator.__call__()
main ends.....
关于上述代码,我无法理解以下几点:
为什么“主要开始......”不是要打印的第一行?
假设如果我从 aFunction() 返回一些值,那么它将无法代替它的调用,因为
aFunction()
它被替换为inside myDecorator.__call__()
notinside aFunction()
。