带参数的装饰器
def login(text):
def decorator(func):
def wrapper(*args, **kargs):
print('%s----%s'%(text, func.__name__))
return func(*args, **kargs)
return wrapper
return decorator
@login('this is a parameter of decorator')
def f2():
print('2020-11-17')
if __name__ == '__main__':
f2()
print(f2.__name__)
执行结果:
this is a parameter of decorator----f2
2020-11-17
wrapper
import functools
def login(text):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kargs):
print('%s----%s'%(text, func.__name__))
return func(*args, **kargs)
return wrapper
return decorator
@login('this is a parameter of decorator')
def f2():
print('2020-11-17')
if __name__ == '__main__':
f2()
print(f2.__name__)
执行结果:
this is a parameter of decorator----f2
2020-11-17
f2