class Decorate(object):
"""将最终返回的函数名更新为func原始函数名"""
def __init__(self, func):
print('装饰器初始化',func.__name__)
self.__func = func
self.__name__ = Decorate.__name__
@classmethod
def set_name(cls,update_func):
cls.__name__ = update_func.__name__
print(cls.__name__)
return cls
def __call__(self, *args, **kwargs):
print('装饰器功能')
self.__func(*args, **kwargs)
def decorater(func):
@Decorate.set_name(func)
def wrapping(*args,**kwargs):
print('执行函数',func.__name__)
return func(*args,**kwargs)
return wrapping
@decorater
def test(a,b):
print(a+b)
test(2,3)
print('返回的函数名',test.__name__)
自写的类似@functools.wraps(func)的类装饰器简单版
最新推荐文章于 2024-03-18 21:21:44 发布