Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Meaning Of '@' before A function In Python 3? [duplicate]

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?

like image 611
Alirezaarabi Avatar asked Oct 19 '25 18:10

Alirezaarabi


1 Answers

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.
like image 118
Sohaib Avatar answered Oct 22 '25 08:10

Sohaib



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!