Re:WTF Python: Exploring and understanding Python through su...
by edflsafoiewq on Thursday February 11, 2021 @06:03AM
(#26100254)
That's how I expect it to behave. But anyway, the alternative (x in a new lexical environment each time the loop body is entered) only makes sense in a language where variables are scoped to a lexical block, not in Python where variables are scoped to the whole function.
Re:WTF Python: Exploring and understanding Python through su...
by iovrthoughtthis on Thursday February 11, 2021 @05:04AM
(#26099917)
I’ve seen a number of experienced python programmers bitten by this. They expect a new scope but don’t get one.
for x in range(7): def some_func(): return x
Here x binds to the (outside) symbol x, not the value of x at creation time of the inner function.