`@` 在 Python 中是什么意思? [英] What does `@` mean in Python?

查看:34
本文介绍了`@` 在 Python 中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@ 在 Python 中是什么意思?

What does @ mean in 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

这只是一种奇特的方式:

Is just a fancy way of doing this:

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

有关更多信息,请查看文档.

For more, check out the documentation.

这篇关于`@` 在 Python 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆