What does "Decorated function definitions do not result in temporary name binding" mean?
I am now studying decorators in Python, and this sentence really confused me.
What does "Decorated function definitions do not result in temporary name binding" mean?
I am now studying decorators in Python, and this sentence really confused me.
I think it's saying that the decorated function is never actually assigned to anything in the namespace before the decorator is applied. When you write a function with a decorator, the function you write is passed to the decorator as an argument, and the object the decorator returns is then treated as the function. Here's a quick example:
def dec(func):
print('times_two' in globals())
def _inner(*args, **kwargs):
print("Decorated")
return func(*args, **kwargs)
return _inner
@dec
def times_two(x):
return x*2
print('times_two' in globals())
You can see this running here. The print in dec says False, because the name "times_two" isn't bound to anything before the decorator is done "decorating" the function.
times_two in the above example such that it doesn't also print "Decorated", accessing the function before the decorator is applied. The current implementation of generators guarantees that this will never happen. That's what the statement in your question is saying. The statement in your comment is an explanation of what a decorator is without touching on the above argument.