Python3 函數04
來自專欄 管道工的日常
#裝飾器def now(): print(12:30)f = nowf()print(f.__name__)12:30now---------------------------------------------------------------------------def log(func): def wrapper(): print(call %s,func.__name__) return func() return wrapper@logdef now(): print(12:30)now()call %s now12:30-------------------------------------------------------------------------def log(func): def inner(): print(call %s,func.__name__) func() return inner@logdef now(): print(12:30)now()call %s now12:30--------------------------------------------------------------------------def check_login(func): def inner(): print(登錄驗證、、、) func() return innerdef fss(): print(發說說)fss = check_login(fss)def ftp(): print(發圖片)ftp = check_login(ftp)btn_index = 2if btn_index == 1: fss()else: ftp()---------------------------------------------------------------------------def check_login(func): def inner(): print(登錄驗證、、、) func() return inner@check_logindef fss(): print(發說說)@check_logindef ftp(): print(發圖片)btn_index = 2if btn_index == 1: fss()else: ftp()---------------------------------------------------------------------------import functoolsdef log(text): def decorator(func): @functools.wraps(func) def wrapper(): print({0}{1}:.format(text,func.__name__)) return func() return wrapper return decorator@log(wahaha)def now(): print(你好)print(now)--------------------------------------------------------------------------from functools import wrapsdef makeitalic(func): @wraps(func) def wrapper(): print(<i>,func(),</i>) return wrapperdef makebold(func): @wraps(func) def wrapper(): print(<b>, func(), </b>) return wrapper@makeitalicdef hello(): return 你好hello()print(hello.__name__)@makebolddef hello(): return 你好hello()print(hello.__name__)<i> 你好 </i>hello<b> 你好 </b>hello--------------------------------------------------------------------------from functools import wrapsdef makeitalic(func): @wraps(func) def wrapper(): return (<i>+func()+</i>) return wrapperdef makebold(func): @wraps(func) def wrapper(): return (<b>+func()+</b>) return wrapper@makebold@makeitalicdef hello(): return 你好print(hello.__name__)print(hello())hello<b><i>你好</i></b>---------------------------------------------------------------------------def makebold(func): def wrapper(): return <b>+func()+</b> return wrapperdef makeitalic(func): def wrapper(): return <i>+func()+</i> return wrapperdef hello(): return (你好)hello = makeitalic(hello)hello = makebold(hello)print(hello())<b><i>你好</i></b>-------------------------------------------------------------------------#函數和面向對象兩座大山
推薦閱讀:
※metaparse - 一個非常易用的Parser
※urllib2添加data和header的代碼
※使用Pandas&NumPy進行數據清洗的6大常用方法
※Python 的類的下劃線命名有什麼不同?
※PY交易(二)使用Pygame寫一個小遊戲——Pie