from functools import wraps ''' #带参函数 def greet_decorator(name): def decorator(func): @wraps(func) def function(): print('包装函数开始执行') func() print('包装函数结束执行') return function return decorator @greet_decorator('name') def hello(msg = 'good'): print('hello'+msg) hello() ''' #带参类 class greet_decoretor(): def __init__(self,name): self.name = name def __call__(self,func): @wraps(func) def wrap_function(*args,**kwargs): print('包装函数开始执行') func(*args,**kwargs) print('包装函数结束执行') return wrap_function @greet_decoretor('name') def hello(): print('hello') hello()
python 装饰器小练习
最新推荐文章于 2024-05-16 08:35:41 发布