我有一个简单的装饰器,输出让我很困惑。
def deco(func):
def kdeco():
print("before myfunc() called.")
func()
print(" after myfunc() called.")
return kdeco
@deco
def myfunc():
print(" myfunc() called.")
myfunc()
before myfunc() called.
myfunc( deco(myfunc)()) called.
after myfunc() called.
deco(myfunc)()
before myfunc() called.
before myfunc() called.
myfunc() called.
after myfunc() called.
after myfunc() called.
我知道 myfunc() 的输出,但是 deco(myfunc)() 的输出让我很困惑,为什么 deco(myfunc)() 的输出不能是下面的任何一个?
状态一:
before myfunc() called.
before myfunc() called.
myfunc() called.
myfunc() called.
after myfunc() called.
after myfunc() called.
状态二:
before myfunc() called.
myfunc( deco(myfunc)()) called.
after myfunc() called.
before myfunc() called.
myfunc( deco(myfunc)()) called.
after myfunc() called.