python-guide icon indicating copy to clipboard operation
python-guide copied to clipboard

"Late-binding" isn't the issue with closures on a loop

Open SoniEx2 opened this issue 5 years ago • 0 comments
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)

SoniEx2 avatar Jan 13 '20 22:01 SoniEx2