Python 第一類物件
阿新 • • 發佈:2018-12-10
def fn(): print("我叫fn") fn() print(fn) # <function fn at 0x0000000001D12E18> fn() gn = fn # 函式名可以進行賦值 print(gn) gn() fn = 666 print(fn) # 666 def func1(): print("朱祁鎮") def func2(): print("徐階") def func3(): print("王陽明") def func4(): print("魏忠賢") lst = [func1, func2, func3, func4] # 函式+() 就是呼叫. print(lst) lst[0]() for el in lst: # el是列表中的每一項. el() # 拿到函式. 執行函式 a = 10 b = 20 c = 30 lst = [a, b, c] print(lst) def wrapper(): def inner(): print("我的天, 還可以扎樣寫") print(inner) # <function wrapper.<locals>.inner at 0x00000000028989D8> inner() return inner ret = wrapper() # <function wrapper.<locals>.inner at 0x00000000028989D8> print(ret) ret() def wrapper(): def inner(): print("哈哈哈") return inner # 函式名可以像返回值一樣返回 ret = wrapper() ret() # 在函式外面訪問了函式內部的函式 ret() ret() def func1(): a = 10 return a print(func1()) # 函式可以作為引數進行傳遞 def func1(): print("謝晉") def func2(): print('楊士奇') def func3(): print('徐渭') def func4(): print("柳如是") # 代理. 裝飾器的雛形 def proxy(a): # a就是變數. 形參 print("我是代理") a() print("代理執行完畢") proxy(func1) proxy(func3) proxy(func4)