0

I'm currently learning Python and I came across a notation I was wondering about:

import taskmanager as tm

#........

@tm.task(str)
def feat(folder):
    BLA BLA BLA ...

the code is a small excerpt from https://github.com/kfrancoi/phd-retailreco/blob/master/libraries/plsa/example_plsa.py (the file contains several more notations using the @ sign). Is this a common notation in python and what does it mean? Or is this only a notation used in this special case with the taskmanager or what!?

I tried my best looking this up on google but it's though to find as the @-sign is stripped out in my search (too short, special character). Same happens here on Stackoverflow.

Thank you very much in advance

tim
  • 9,896
  • 20
  • 81
  • 137
  • 1
    **AS I DESCRIBED IN THE QUESTION** I could **NOT** find a related question as the @-sign is stripped out and didn't know what to look for otherwise (in this case, for decorator which is completely NEW to me...) – tim Jun 10 '14 at 11:52

1 Answers1

3

This a decorator, defined by the PEP 318. Extract of the glossary:

A function returning another function, usually applied as a function transformation using the @wrapper syntax. Common examples for decorators are classmethod() and staticmethod().

The decorator syntax is merely syntactic sugar, the following two function definitions are semantically equivalent:

def f(...):
    ...
f = staticmethod(f)

@staticmethod
def f(...):
    ...

The same concept exists for classes, but is less commonly used there. See the documentation for function definitions and class definitions for more about decorators.

Related: What are some common uses for Python decorators?

Community
  • 1
  • 1
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
  • Thanks for your answer. Again: I really do not understand that people downvote the question. **AS I DESCRIBED IN THE QUESTION** I could NOT found a related question as the @-sign is stripped out and didn't know what to look for otherwise (in this case, for *decorator* which is completely NEW to me...). Thanks thuogh! – tim Jun 10 '14 at 11:48
  • You could have searched with *arobase* instead of `@` :-) – Maxime Lorant Jun 10 '14 at 11:50
  • Haha sure, completely new word for me... but nevermind, you even answered, thanks! – tim Jun 10 '14 at 11:51