python lines 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?


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


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.


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)
链接地址: http://www.djcxy.com/p/23798.html

上一篇: 在Python中@和lambda是什么意思?

下一篇: 以@开头的python行