装饰器
code
import time def time_count(func): def wrapper(*args, **kwargs): start = time.time() res = func(*args, **kwargs) end = time.time() print(f'{func} time is {start - end}') return res return wrapper @time_count # home = time_count(home) def home(name): print(f'hello {name} my brother') time.sleep(1) return name res = home('egon') print(f'res:{res}')
outputs
macname@MacdeMacBook-Pro py % python3 cccccc.py hello egon my brother <function home at 0x10ad880e0> time is -1.0011210441589355 res:egon macname@MacdeMacBook-Pro py %