1

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
  • 8,910
  • 6
  • 33
  • 52
Alirezaarabi
  • 282
  • 6
  • 23

1 Answers1

2

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)
args_funtion()
Sohaib
  • 528
  • 5
  • 14