I am reading this article about decorator.
At Step 8 , there is a function defined as:
def outer():x = 1def inner():print x # 1return inner
and if we run it by:
>>> foo = outer()
>>> foo.func_closure # doctest: +ELLIPSIS
it doesn't print x. According to the explanation :
Everything works according to Python’s scoping rules - x is a localvariable in our function outer. When inner prints x at point #1 Pythonlooks for a local variable to inner and not finding it looks in theenclosing scope which is the function outer, finding it there.
But what about things from the point of view of variable lifetime? Ourvariable x is local to the function outer which means it only existswhile the function outer is running. We aren’t able to call inner tillafter the return of outer so according to our model of how Pythonworks, x shouldn’t exist anymore by the time we call inner and perhapsa runtime error of some kind should occur.
However, I don't really understand what the second paragraph means.
I understand inner() does get the value of x but why it doesn't print x out?
thanks
UPDATE:
Thanks all for the answers. Now I understand the reason. the "return inner" is just a pointer to inner() but it doesn't get executed, that is why inner() doesn't print x as it is not called at all