News for hackers. Stuff that matters.
Re:WTF Python: Exploring and understanding Python through su... by singularity2001 on Thursday February 11, 2021 @04:53AM (#26099853)

  >> none of the code on this page is something anyone would actually write in production code on purpose.
I mostly agree, but there is one example where I thought it is important to know and remember:

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.

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.
Re:WTF Python: Exploring and understanding Python through su... by pansa2 on Thursday February 11, 2021 @04:57AM (#26099867)
Yes - in many languages x is a new variable on each iteration of the loop. Python re-uses the same variable each time, because the loop does not have its own scope.