Python裝飾器
裝飾器用來裝飾函式
返回一個函式物件
“被裝飾函式識別符號”指向“返回的函式物件”
語法糖 @dec
裝飾器基本構造流程(類比閉包)
#裝飾器是對閉包原理的應用
def dec(func):
print('CALL DEC')
def in_dec(*arg):
print('in_dec arg = :',arg) #測試arg引數呼叫
if len(arg)==0:
return 0
for val in arg:
if not isinstance(val,int):
return 0
return func(*arg)
return in_dec
@dec #@dec的作用等價於my_sum=dec(my_sum)
def my_sum(*arg):
print('in my_sum')
return sum(arg)
#Output:CALL DEC
print(my_sum(1,2,3,4,5))
#Output:in_dec arg = : (1, 2, 3, 4, 5)
# in my_sum
# 15
被裝飾函式的返回值
def deco(func):
def in_deco():
print('in deco')
func()
print('call deco')
return None
@deco #deco(bar)==>返回None,bar=None
def bar():
print('in bar')
print(type(bar))
#Output:call deco
# <class 'NoneType'>
def deco(func):
def in_deco():
print('in deco')
func() #bar引數傳遞給func,賦值給閉包in_deco的closure屬性
print('call deco')
return in_deco
@deco #deco(bar)==>返回in_decp,bar指向in_decp這個函式
def bar():
print('in bar')
print(type(bar))
bar()
#Output:call deco
# <class 'function'>
# in deco
# in bar
“有引數的裝飾器”構造
def deco(func):
def in_deco(x,y):
print('in deco')
func(x,y) #bar引數傳遞給func,賦值給閉包in_deco的closure屬性
print('call deco')
return in_deco
###############################
#deco(bar)==>in_deco
#bar=in_deco
#bar() indeco()-->bar()
###############################
@deco #deco(bar)==>返回in_decp,bar指向in_decp這個函式
def bar(x,y):
print('in bar',x+y)
print(type(bar))
bar(1,2)
#Output:call deco
# <class 'function'>
# in deco
# in bar 3
閉包參考https://blog.csdn.net/cymy001/article/details/78125192
#閉包
def my_sum(*arg):
print('in my_sum')
return sum(arg)
def dec(func):
def in_dec(*arg):
print('in_dec arg = :',arg) #測試arg引數呼叫
if len(arg)==0:
return 0
for val in arg:
if not isinstance(val,int):
return 0
return func(*arg)
return in_dec
my_sum=dec(my_sum)
#呼叫dec,返回in_dec函式;在in_dec閉包裡引用了傳遞的my_sum函式
#用dec(my_sum)的返回值給my_sum賦值,my_sum指向in_dec
#所以,呼叫my_sum時,首先呼叫in_dec;然後呼叫傳遞給func的引數my_sum
print(my_sum(1,2,3,4,5))
print(my_sum(1,2,3,4,5,'6'))
#Output:in_dec arg = : (1, 2, 3, 4, 5)
# in my_sum
# 15
# in_dec arg = : (1, 2, 3, 4, 5, '6')
# 0
#呼叫順序:step1,呼叫in_dec函式;step2,呼叫func引數對應的my_sum函式;
#即所有引數先交給in_dec處理,再由my_sum進行計算
相關文章
- python裝飾器2:類裝飾器Python
- Python 裝飾器Python
- 1.5.3 Python裝飾器Python
- Python 裝飾器(一)Python
- python的裝飾器Python
- Python 裝飾器原理Python
- Python裝飾器模式Python模式
- Python深入05 裝飾器Python
- python 之裝飾器(decorator)Python
- Python中的裝飾器Python
- Python裝飾器詳解Python
- python裝飾器入門探究Python
- python 裝飾器 part2Python
- day11(python)裝飾器Python
- python中裝飾器的原理Python
- Python裝飾器高階用法Python
- python 裝飾器小白學習Python
- 【python】閉包與裝飾器Python
- Python裝飾器的前世今生Python
- python的裝飾器@的用法Python
- Python深入分享之裝飾器Python
- Python 語法之裝飾器Python
- Python3 裝飾器解析Python
- Python閉包與裝飾器Python
- Python之函式裝飾器Python函式
- Python 裝飾器簡單示例Python
- python裝飾器有哪些作用Python
- python裝飾器是什麼Python
- 粗淺聊聊Python裝飾器Python
- Python迭代器&生成器&裝飾器Python
- python幾種裝飾器的用法Python
- Python學習筆記 - 裝飾器Python筆記
- Python 裝飾器你也會用Python
- Python:從閉包到裝飾器Python
- python中的裝飾器介紹Python
- python3 裝飾器全解Python
- Python設計模式-裝飾器模式Python設計模式
- 瞭解python的裝飾器特性Python