Skip to content

杂记 (类装饰器, ) - 20240806

类装饰器

利用 Python 万物皆对象的设计理念, 和 class __call__ 方法, 制作装饰器

py
class Decorator:
    def __init__(self, func):
        self.__func = func

    # 调用时运行
    def __call__(self, *args, **kwargs):
        print('before')
        self.__func(*args, **kwargs)
        print('after')