1

Just a quick question,

Could someone link me to the documentation for the use of @ in python?

Due not being able to google @ and not knowing the use or name of it I'm not sure how to find it on my own :)

Many thanks!!

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
GP89
  • 6,600
  • 4
  • 36
  • 64
  • 1
    Very similar question to http://stackoverflow.com/questions/5337890/python-at-prefix-in-top-level-module-code-what-does-it-stand-for/5337982#5337982 – eat Apr 01 '11 at 14:03

4 Answers4

8

Symbols starting with "@" (e.g. @staticmethod) are called "decorators" in Python jargon.

You can find the PEP describing them at this url.

In short, they are syntactic sugar to invoke a function over the object being decorated, e.g.:

@staticmethod
def myfunc(...): ...

is equivalent to:

def myfunc(...): ...
myfunc = staticmethod(myfunc)

Then, searching on the web for "python decorator" will provide you with a lot of other useful information and use cases.

Hope it helps, ciao

masterpiga
  • 860
  • 7
  • 13
  • Also in the python docs [glossary page](http://docs.python.org/glossary.html#term-decorator) and discussed under [function definitions](http://docs.python.org/reference/compound_stmts.html#function) – coltraneofmars Apr 01 '11 at 14:00
4

Python decorators:

http://docs.python.org/reference/compound_stmts.html#function

steinar
  • 9,383
  • 1
  • 23
  • 37
2

Google for python decorator and you will find enough answers to your question.

Ocaso Protal
  • 19,362
  • 8
  • 76
  • 83
0

As other people said, they are decorators. They take the decorated object as an argument and return a new object - which should usually be the same type as the initial one (function or class)

If you do not like the at syntax, you can always write it like that:

@foo
def bar():
    pass

# is the same as:
def bar():
    pass
bar = foo(bar)
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636