I don't think you understand decorators. Let's make a minimal example.
def my_decorator(some_function):
def new_function(*args, **kwargs):
'announces the result of some_function, returns None'
result = some_function(*args, **kwargs)
print('{} produced {}'.format(some_function.__name__, result))
return new_function # NO FUNCTION CALL HERE!
@my_decorator
def my_function(a, b):
return a + b
my_function(1, 2) # will print "my_function produced 3"
We have a simple function my_function
which returns the sum of its two arguments and a decorator which will just print out the result of whatever function it decorates.
Note that
@my_decorator
def my_function(a, b):
return a + b
is equivalent to
def my_function(a, b):
return a + b
my_function = my_decorator(my_function)
Since my_decorator
accepts a function as an argument (here we are giving it my_function
) and returns a new function new_function
(without calling it!), we effectively override my_function
because we reassign the name to whatever my_decorator
returns.
In action:
>>> my_function(1, 2)
my_function produced 3
Note that at every point in the example when a function is called, it happens with the parentheses-syntax. Here are all the function calls that happen in the first block of code I posted, in order:
my_decorator(my_function)
is called and the return value is reassigned to the name my_function
. This either happens through the @
syntax or more explicitly in the equivalent code snippet.
my_function(1, 2)
is called. At this point, my_function
is the new_function
that got returned by the decorator. Brain-parse it as new_function(1, 2)
.
- Inside the body of
new_function
, the argument we gave to my_decorator
is called (result = some_function(*args, **kwargs)
) which happens to be the value of my_function
before the reassignment that happened in step 1.
print
is called.
If you want to understand how new_function
is holding on to some_function
despite my_decorator
already having returned from its call, I suggest looking into the topics free variables and closures.