0

Why does x[i]() return 9 for any i, preceded by this code?

x = []
for i in range(1,10):
    x += [lambda: i]
rishat
  • 8,206
  • 4
  • 44
  • 69
  • Execution of a function is **always** deferred. You are asking about why looking up `i` is deferred. The answer: closures. – Martijn Pieters Oct 29 '14 at 22:27
  • Are you asking for an explanation of why the code does what it does, or an explanation of the language design considerations that lead to Python having that behavior? – BrenBarn Oct 29 '14 at 22:28

0 Answers0