装饰器如何在没有正式调用的情况下注册一个函数?

How a decorator can register a function without a formal call?

我正在查看 here 中的示例之一,特别是以下示例:

import random
PLUGINS = dict()

def register(func):
    """Register a function as a plug-in"""
    PLUGINS[func.__name__] = func
    return func

@register
def say_hello(name):
    return f"Hello {name}"

@register
def be_awesome(name):
    return f"Yo {name}, together we are the awesomest!"

def randomly_greet(name):
    greeter, greeter_func = random.choice(list(PLUGINS.items()))
    print(f"Using {greeter!r}")
    return greeter_func(name)

令我困惑的是 say_hello()be_awesome() 函数直到最后一行都没有被调用,但是 PLUGINS 已经包含了它们。我习惯于认为应用装饰器 显式调用函数,但是这个例子告诉我我错了。发生这种情况是因为 register 函数没有调用 func 吗?但是,如果它确实调用了怎么办?这是需要记住的特例还是背后有某种系统?

@register
def say_hello(name):
    return f"Hello {name}"

相当于:

def say_hello(name):
    return f"Hello {name}"

say_hello = register(say_hello)