裝飾器的基本實現:函數
def kuozhan(func):ui
def newruixin():擴展
#擴展功能:
print('若是我說若是‘)裝飾器
#調用基本函數new
funcreturn
print('沒有若是‘)參數
#增長返回值
return newruixin
#基本函數
@kuozhan #ruixin = kuozhan(ruixin) 擴展以後的函數要從新賦值給基本函數,
def ruixin():
print('很難實現‘)
#調用基本函數
ruixin()
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# 和帶有返回值的裝飾器
def kuozhann(func):
def newruixin():
print('若是沒有若是')
result=func()
print('哈哈哈哈哈‘)
return result
return newruixin
@kuozhan
def ruixin():
print('必定沒有若是‘)
return '嘿嘿嘿嘿‘
ruixin()
---------------------------------------------------------------------------------------------------------------------------------------------
#帶有參數的裝飾器
def outer(arg):
def kuozhan(func):
def newruixin():
print('111111111')
func()
print('22222222')
def newfanbo():
print('aaaaaaaaaaa')
func()
print('bbbbbbbb')
if arg =='ruixin':
return newruixin
elif arg =='fanbo'
return newfanbo
return kuozhan
@outer('ruixin')
def ruixin():
print('111222')
@outer('fanbo')
print('asasasa')
fanbo()
ruixin()