python-guide
python-guide copied to clipboard
"Late-binding" isn't the issue with closures on a loop
trafficstars
Lua also has late-binding closures, as can be seen here: https://repl.it/repls/HotpinkEagerQuery (the first one intentionally misused to highlight the difference between block scope and function scope)
It just also has block scope. That's the thing python doesn't have, thus causing the confusion.
Block scope is better emulated with nested functions, such as:
for i in range(5):
def block_scope_emulation(i):
def thing(x):
return x*i
block_scope_emulation(i)