还将定义另一个函数的装饰器

Decorator that will also define another function

我有一个 class,其中包含一个“功能”列表,其中每个功能都是在 class 中找到的一个功能的名称。这是一种动态控制将哪些功能添加到时间序列数据帧的方法。我在另一个 class 中有一个函数,它旨在获取现有特征并检索其未来状态。所以根据当前结构的设置方式,我需要通过在其名称末尾附加“_after”来动态添加一个函数。

到目前为止,我已经设置了装饰器,功能列表已更新为新的函数名称,但我不知道如何从装饰器中声明附加函数。最终我什至不需要包装原始函数,我只需要使用旧函数的命名约定创建一个新函数。

在这个人为的例子中,Dog 最终应该有两个函数:bark()bark_after()Features 列表应包含 ['bark', 'bark_again']。我也不想将 Features 显式传递给装饰器。

class Dog:
    Features = ['bark']

    def __init__(self, name):
        self.name = name
        
    def gets_future_value(*args):
        def decorator(function):
            new_function = f'{function.__name__}_after'
            args[0].append(new_function)
            return function
        return decorator
    
    @gets_future_value(Features)
    def bark(self):
        print('Bark!')

d = Dog('Mr. Barkington')
print(d.Features)
d.bark()
d.bark_after()

我想你需要的是 class decorator:

import inspect


def add_after_methods(cls):
    features = set(cls.Features)  # For fast membership testing.
    isfunction = inspect.isfunction

    def isfeature(member):
        """Return True if the member is a Python function and a class feature."""
        return isfunction(member) and member.__name__ in features

    # Create any needed _after functions.
    for name, member in inspect.getmembers(cls, isfeature):
        after_func_name = name + '_after'
        def after_func(*args, **kwargs):
            print(f'in {after_func_name}()')
        setattr(cls, after_func_name, after_func)
        cls.Features.append(after_func_name)
    return cls


@add_after_methods
class Dog:
    Features = ['bark']

    def __init__(self, name):
        self.name = name

    def bark(self):
        print('Bark!')


d = Dog('Mr. Barkington')
print(d.Features)  # -> ['bark', 'bark_after']
d.bark()  # -> Bark!
d.bark_after()  # -> in bark_after()