装饰器函数 装饰一个类的实例的方法(类的函数)

def decorator(func):
    print('func= ', func)
    
    def warp(*args, **kwargs):
        print('args=  ', args)
        return func(*args, *kwargs)
    return warp


class AAA:
    def __init__(self, name):
        print('self== ', self)
        self.name = name
    
    @decorator
    def get_args(self, args):
        print(f'get_args self and args== ', self, args)
        
    # def get_args(self, args):
    #     print(f'get_args self and args:', self, args)
    # print('get_args== ', get_args)
    # get_args = decorator(get_args)


if __name__ == '__main__':
    pass
    print('AAA.get_args after call=== ', AAA.get_args)
    aaa = AAA('zhangsan')
    aaa.get_args('args_string')
    print(aaa.get_args)

func= <function AAA.get_args at 0x000001D7510AF9A0>
AAA.get_args after call=== <function decorator.<locals>.warp at 0x000001D7510AFB50>
self== <__main__.AAA object at 0x000001D751092D70>
args= (<__main__.AAA object at 0x000001D751092D70>, 'args_string')
get_args self and args== <__main__.AAA object at 0x000001D751092D70> args_string
<bound method decorator.<locals>.warp of <__main__.AAA object at 0x000001D751092D70>>

posted @ 2023-10-18 23:57  fangpinz  阅读(6)  评论(0编辑  收藏  举报