2

I searched for the meaning of @ when it comes before a function in Python and I couldn't find a helpful thing.

For example, I saw this code in Django:

@login_required

...and this in the goto-statement package:

@with_goto

What does it mean?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Alirezaarabi
  • 348
  • 8
  • 25

1 Answers1

4

It represent the Decorator. A decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.

def decorator_function(func):
    def inner_function():
        print("Before the function is called.")
        func()
        print("After the function is called.")
    return inner_function

@decorator_function
def args_funtion():
    print("In the middle we are!")

Actually this @decorator_function decorator perform the same job as:

args_funtion = decorator_function(args_funtion)

And now if you call it this would be the result:

>>> args_funtion()
Before the function is called.
In the middle we are!
After the function is called.
Community
  • 1
  • 1
Sohaib
  • 566
  • 6
  • 15