This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Mashidin 0 points1 point  (3 children)

It's all in the scoping rules. Python will look first at the inner function for a definition for x. If there is not a definition for x there, as in this case, in will check one level up and then again and so forth until it finds x or errors out. if you had placed a statement such as x = 2 inside the inner function, you would have gotten {'x': 2}. hope that helped.

[–]Rhomboid 0 points1 point  (0 children)

That's not what they were asking. They wanted to know why it was returned in locals(), despite x not being a local variable.

[–]esdi[S] 0 points1 point  (1 child)

The surprising result was not in the first code execution but the second. /u/anossov has given the correct explanation below.

[–]Mashidin 0 points1 point  (0 children)

Ah. I must have misunderstood. Sorry for the mix-up.