2

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?

smci
  • 32,567
  • 20
  • 113
  • 146
Augie
  • 123
  • 5
  • http://stackoverflow.com/questions/739654/understanding-python-decorators –  Jul 15 '11 at 18:46
  • 1
    +1 because it is a valid question, because you don't know, that you have to search for decorators when you see the `@` symbol the first time. – Framester Apr 23 '15 at 13:57
  • Also the right duplicate would be http://stackoverflow.com/questions/6392739/what-does-the-at-symbol-do-in-python – Framester Apr 23 '15 at 13:59

4 Answers4

5

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.

Gabi Purcaru
  • 30,940
  • 9
  • 79
  • 95
4

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)
Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
3

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

Community
  • 1
  • 1
TorelTwiddler
  • 5,996
  • 2
  • 32
  • 39
0

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

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

activities = login_required(activities)
utdemir
  • 26,532
  • 10
  • 62
  • 81