装饰方法赋值前引用的局部变量

Local variable referenced before assignment in decorated method

我正在创建一个装饰器,它允许我做类似的事情:

@cooldownf(lambda self, **eargs: 30 - self.level)
def method(self, **eargs):
    ...

这将简单地修饰该方法,使其具有冷却时间。 这一切都很好,现在可以每 30 - self.level 秒执行一次该方法。

但是,我想添加一条消息,如果该方法仍处于冷却状态,则会显示该消息。 我为 cooldownf 装饰器添加了一个 message 参数,但随后我从装饰器代码中的 ìf message: 行收到错误 UnboundLocalError: local variable 'message' referenced before assignment

def cooldownf(fn, message=None):
    """Decorates a method to have a dynamic cooldown.

    Decorator function for easily adding cooldown as a dynamic time
    (function) into skill's methods. The function gets called when the
    cooldown is needed, and the skill is passed to the function.

    Args:
        fn: Function to determine the cooldown of the method
        message: Optional message sent if there's still cooldown left

    Returns:
        Decorated method with a dynamic cooldown
    """

    # Create a decorator using the function and message provided
    def method_decorator(method):

        # Create a wrapper method
        @wraps(method, assigned=WRAPPER_ASSIGNMENTS+('__dict__',), updated=())
        def method_wrapper(self, **eargs):

            # If the method's cooldown is over
            if method_wrapper.cooldown.remaining <= 0:

                # Restart the cooldown
                method_wrapper.cooldown.start(1, fn(self, **eargs))

                # And call the function
                return method(self, **eargs)

            # If there was cooldown remaining and a message is provided
            if message:

                # Format the provided message
                message = message.format(
                    name=self.name,
                    cd=method_wrapper.cooldown.remaining,
                    max_cd=method_wrapper.cooldown.limit
                )

                # Send it to the player
                SayText2(message=message).send(eargs['player'].index)

                # And exit with code 3
                return 3

        # Create the cooldown object for the wrapper
        method_wrapper.cooldown = TickRepeat(lambda: None)

        # And return the wrapper
        return method_wrapper

    # Return the decorator
    return method_decorator

这是什么原因造成的? 我可以在 cooldownfmethod_decorator 中打印 message,但在 method_wrapper 中添加打印会导致出现错误。 这是确切的代码,我无法在 IDLE 中使用函数复制它,这与我使用 方法 有什么关系吗?

您正在分配给最内层函数中的message

message = message.format(
    name=self.name,
    cd=method_wrapper.cooldown.remaining,
    max_cd=method_wrapper.cooldown.limit
)

该赋值使其成为一个局部变量,但您必须先访问 message 才能进行该赋值。你不能用本地人做到这一点。

由于您不想修改封闭参数,因此您想在此处使用 本地名称:

formatted_message = message.format(
    name=self.name,
    cd=method_wrapper.cooldown.remaining,
    max_cd=method_wrapper.cooldown.limit
)
SayText2(message=formatted_message).send(eargs['player'].index)