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 ##調用裝飾器 也等于 aa = aa(fun) def aa():time.sleep(3)print('it is in aa')aa() View Code?升級版 ? 裝飾器
#!/bin/python # -*-coding:utf-8-*- import time def fun(abc):print('it is %s'%abc)def outer(bian):###裝飾器def fun_1(*args,**kwargs): ##定義自定義參數符合任何 函數的 功能調用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 只有這樣 引用函數如有 return 的話也會顯示return fun_1return outer @fun(abc='local1') ##調用裝飾器 也等于 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'就會顯示 如不 rec的話 返回是 None
?
轉載于:https://www.cnblogs.com/th-lyc/p/8557801.html
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
- 上一篇: 【图数据结构的遍历】java实现广度优先
- 下一篇: 字符序列