I am reading this article about a decorator.
In Step 8, there is a function defined as:
def outer(): x = 1 def inner(): print x
and if we run it:
>>> foo = outer() >>> foo.func_closure
it does not print x. According to the explanation:
Everything works according to the rules of the review. Pythons - x is a local variable in our external function. When the inner print of x at point # 1, Python searches for a local variable in the inside and does not find it in the scope, which is an external function, finding it there.
But what about things in terms of variable lifetime? our variable x is local to the external function, which means that it exists only and the external function works. We can name internal before after returning external, so according to our model of how Python works, x should not exist anymore by the time we call internal and, possibly, a runtime error should occur.
However, I do not quite understand what the second paragraph means.
I understand that inner () really gets the value of x, but why doesn't it print x out?
thanks
UPDATE
Thanks everyone for the answers. Now I understand the reason. " return inner " is a pointer for internal (), but it fails, so inner () does not print x, since it is not called at all
source share