Why do Python yield statements form a closure?
I have two functions that return a list of functions. The functions take in a number x and add i to it. i is an integer increasing from 0-9. def test_without_closure(): return [lambda x: x+i for i in range(10)] def test_with_yield(): for i in range(10): yield lambda x: x+i I would expect test_without_closure to return a list of 10 functions that each add 9 to x since i 's value is 9 . print sum(t(1) for t in test_without_closure()) # prints 100 I expected that test_with_yield would also have the same behavior, but it correctly creates the 10 functions. print sum(t(1) for t in test_with_yield()