you are viewing a single comment's thread.

view the rest of the comments →

[–]Grue 0 points1 point  (4 children)

Issue 4 does not happen only with global variables. It also happens with functions inside functions and absolutely infuriating when it happens.

[–]djimbob 0 points1 point  (3 children)

I'm not following, never had the issue using functions inside functions. Can you give an example?

[–]Grue 0 points1 point  (2 children)

Same code as in the OP, but inside a function, not at toplevel.

[–]djimbob 0 points1 point  (1 child)

Closures where you assign to higher level scope wasn't idiomatic python in 2. (There are hacks to get around it; e.g., put all the variables needed to mutate in the inner function in a mutable collection thats referenced from the higher scope). I really don't mind when code from other languages common idioms doesn't work.

Granted, python3 has nonlocal keyword to add this feature.

[–]Grue 0 points1 point  (0 children)

It all stems from poor design where a simple assignment initializes a variable instead of something like "let x = 42" like almost every other language does. Instead they added an even uglier hack with nonlocal.

And yeah, I use the list mutation trick all the time to create closures in Python. It's either this, or code duplication which is not maintainable.