1. 程式人生 > >python裝飾器(備忘)

python裝飾器(備忘)

code -- urn decorator args war return ret fun

# 裝飾器decorator

def deco1(fun):
def PRINT(*args,**kwargs):
print(‘------deco1------‘)
fun(*args,**kwargs)
print(‘-----deco1 end------‘)
return PRINT

def deco2(fun):
def PRINT(*args,**kwargs):
print(‘------deco2------‘)
fun(*args,**kwargs)
print(‘-----deco2 end------‘)
return PRINT


@deco2
@deco1 #從下往上裝飾
def fun(STR):
print("fun({})".format(STR))

fun("略略略")

‘‘‘

------deco2------
------deco1------
fun(略略略)
-----deco1 end------
-----deco2 end------

‘‘‘

python裝飾器(備忘)