gpt4 book ai didi

python - `@` 在 Python 中是什么意思?

转载 作者:太空宇宙 更新时间:2023-11-03 12:11:47 25 4
gpt4 key购买 nike

@ 在 Python 中是什么意思?

示例:@login_required

最佳答案

装饰器语法。

A function definition may be wrapped by one or more decorator expressions. Decorator expressions are evaluated when the function is defined, in the scope that contains the function definition. The result must be a callable, which is invoked with the function object as the only argument. The returned value is bound to the function name instead of the function object. Multiple decorators are applied in nested fashion.

所以做这样的事情:

@login_required
def my_function():
pass

这只是一种奇特的方式:

def my_function():
pass
my_function = login_required(my_function)

有关更多信息,请查看 the documentation .

关于python - `@` 在 Python 中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1053732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com