以 @ 开头的 python 行

发布于 2024-11-24 04:05:53 字数 405 浏览 0 评论 0原文

可能的重复:
了解 Python 装饰器

我正在阅读 django 应用程序源代码,在其中我找到了

@login_required
def activities(request = None,\
            project_id = 0,\
            task_id = 0,\
            ...

以 What does the line that start with @ 意思是?

Possible Duplicate:
Understanding Python decorators

I was reading a django app source code where I find this

@login_required
def activities(request = None,\
            project_id = 0,\
            task_id = 0,\
            ...

What does the line that start with @ mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡莣 2024-12-01 04:06:07

它是一个装饰器,是 Python 中一种特殊类型的函数(在某些情况下是类),可以修改另一个函数的行为。请参阅本文

@decorator
def my_func():
    pass

实际上只是一个特殊的语法

def my_func():
    pass
my_func = decorator(my_func)

It's a decorator, which is a special type of function (or class, in some cases) in Python that modifies another function's behavior. See this article.

@decorator
def my_func():
    pass

is really just a special syntax for

def my_func():
    pass
my_func = decorator(my_func)
那支青花 2024-12-01 04:06:07

请查看Python 装饰器解释。它有一个令人惊奇的答案,可以解释一切。

Please check out Python Decorators Explained. It has an amazing answer that will explain everything.

笑叹一世浮沉 2024-12-01 04:06:07

它是一个装饰器。它是一种合成糖,用于:

def activities(request = None,\
            project_id = 0,\
            task_id = 0,\
            ...

activities = login_required(activities)

It is a decorator. It's a syntatic sugar for:

def activities(request = None,\
            project_id = 0,\
            task_id = 0,\
            ...

activities = login_required(activities)
恏ㄋ傷疤忘ㄋ疼 2024-12-01 04:06:06

这是一个装饰器。它所做的基本上就是包装函数。它与此代码等效:

def activities(request = None,\
            project_id = 0,\
            task_id = 0,\
            ...
activities = login_required(activities)

它用于检查函数参数(在本例中为 request.session )、修改参数(它可能会为函数提供除传递之外的其他参数),也许还有其他一些东西。

It's a decorator. What it does is basically wrap the function. It is equivalent with this code:

def activities(request = None,\
            project_id = 0,\
            task_id = 0,\
            ...
activities = login_required(activities)

It is used for checking function arguments (in this case request.session), modifying arguments (it may give the function other arguments than it passes), and maybe some other stuff.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文