3-13 装饰器
?
?
?
?
import time def fun(bian): ###裝飾器def fun_1(): ##定義start_time = time.time()bian()stop_time = time.time()print('it is run %s'%(stop_time-start_time))return fun_1 @fun ##調(diào)用裝飾器 也等于 aa = aa(fun) def aa():time.sleep(3)print('it is in aa')aa() View Code?升級(jí)版 ? 裝飾器
#!/bin/python # -*-coding:utf-8-*- import time def fun(abc):print('it is %s'%abc)def outer(bian):###裝飾器def fun_1(*args,**kwargs): ##定義自定義參數(shù)符合任何 函數(shù)的 功能調(diào)用rec = bian(*args,**kwargs) ### 111if abc == 'local1':start_time = time.time()print('it is local11111111111')stop_time = time.time()print('it is run %s'%(stop_time-start_time))print('****'.center(20,'-'))else:print('gao mao xian a....')return rec ###111 只有這樣 引用函數(shù)如有 return 的話也會(huì)顯示return fun_1return outer @fun(abc='local1') ##調(diào)用裝飾器 也等于 aa = aa(fun) def aa(it):time.sleep(3)print('it is in aa%s'%it) @fun(abc = 'local2') def bb():time.sleep(1)print('hello ')return 'welcome' aa('cc') bb()print(bb()) ###111這樣 'welcome'就會(huì)顯示 如不 rec的話 返回是 None
?
轉(zhuǎn)載于:https://www.cnblogs.com/th-lyc/p/8557801.html
超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
- 上一篇: 【图数据结构的遍历】java实现广度优先
- 下一篇: 字符序列