import functools
def log(text): def decorator(fnc): @functools.wraps(fnc) def wrapper(args,**kw): print('in fnction:%s' %(fnc.name) ) return fnc(args,**kw) return wrapper if callable(text): return decorator(text) else: print('text:%s' %text) return decorator
@log
def f1(): print('f1')
@log('execute')
def f2(): print('f2')
f1() f2()
text:execute in fnction:f1 f1 in fnction:f2 f2
Sign in to make a reply
王加昭53882
import functools
def log(text): def decorator(fnc): @functools.wraps(fnc) def wrapper(args,**kw): print('in fnction:%s' %(fnc.name) ) return fnc(args,**kw) return wrapper if callable(text): return decorator(text) else: print('text:%s' %text) return decorator
@log
def f1(): print('f1')
@log('execute')
def f2(): print('f2')
f1() f2()
运行结果如下:
text:execute in fnction:f1 f1 in fnction:f2 f2