-1

I was seeing the methods and docs of the built in super() method of python using the help() function in the IDLE . I came across this piece of code

This works for class methods too: | class C(B): | @classmethod | def cmeth(cls, arg): | super().cmeth(arg)

In the second line , you can see the @ sign before classmethod .

What does the @ symbol does in python and what are its uses ?

pppery
  • 3,731
  • 22
  • 33
  • 46
Jdeep
  • 1,015
  • 1
  • 11
  • 19

1 Answers1

3

The @ character denotes a decorator. Decorators are functions that can modify or extend behavior of another function temporarily by wrapping around them.

Decorators wrap around a function by receiving them as a parameter. The @ syntax (also known as "pie" syntax) applies the classmethod decorator to cmeth after it is defined in your snippet.

You can read more about the specific decorator from your example (classmethod) here.

siraj
  • 309
  • 2
  • 7
  • 2
    The decorator syntax is not "shorthand for `classmethod(cmeth(cls, arg))`"; that would be applying `classmethod` to the _result_ of calling `cmeth`. The decorator instead applies `classmethod` to `cmeth` itself. That is, applying the `@classmethod` decorator to `cmeth` is syntatic sugar for doing `cmeth = classmethod(cmeth)` _after_ the `cmeth` definition. – Mark Dickinson May 02 '20 at 09:06