我想了解裝飾器的功能。我在下面的代碼中做錯了什麼。請糾正它python中的裝飾器
正如我瞭解,當aFunction()被調用時,它又調用myDecorator(),它也調用afunction()。對?
另外如何傳遞參數代入機能缺失()
class myDecorator(object):
def __init__(self, f):
print "inside myDecorator.__init__()"
f(1) # Prove that function definition has completed
def __call__(self):
print "inside myDecorator.__call__()"
@myDecorator
def aFunction(*a):
print a
print "inside aFunction()"
print "Finished decorating aFunction()"
aFunction(2)
詳盡的解釋:http://stackoverflow.com/questions/739654/understanding-python-decorators – rplnt
如果您需要裝飾和註解更多的幫助,在這裏看到我的博客文章。 http://blog.mattalcock.com/2013/1/5/decorates-and-annotations/ –