def func1(function): print("這裡是執行function()函數之前") def wrapper(): function() wrapper() print ("這裡是執行function ()函數之後") @func1 def func2(): print ("正在執行 function ()函數")
>>> def func1(function):
... print("這裡是執行function()函數之前")
... def wrapper():
... function()
... wrapper()
... print ("這裡是執行function()函數之後")
>>> @func1
def func2():
. print ("正在執行 function ()函數")
這裡是執行function()函數之前
正在執行 function()函數
這裡是執行function()函數之後
@func1
等效於func1(func2)
def func1(arg = True): def func2(): print("This is func2() function") def func3(): print("This is func3() function") if arg == True: return func2 else : return func3 func1()()上述程式碼的執行結果如下所示。
>>> def func1(arg = True):
... def func2():
... print("This is func2() function")
... def func3():
... print("This is func3() function")
... if arg == True:
... return func2
... else :
... return func3
>>> func1()()
This is func2() function
>>> def func1(arg = True):
... def func2():
... print("This is func2() function")
... def func3():
... print("This is func3() function")
... if arg == True:
... return func2
... else :
... return func3
>>> func1()
<function func1.<locals>.func2 at 0x0000014ED8D663A0>
@a
@b
@c
def f()
pass
f = a(b(c(f)))