by ot · 14y ago · 0 comments
In [10]: lambdas = [(lambda: i) for i in xrange(10)]
In [11]: [f() for f in lambdas]
Out[11]: [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
In [12]: lambdas_gen = ((lambda: i) for i in xrange(10))
In [13]: [f() for f in lambdas_gen]
Out[13]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
The problem was not fixed even in Python 3. I don't know if there has been any discussion about it. lambdas = [(lambda: i) for i in xrange(10)]
This works, because it is a proper closure: def build_closure(i):
return (lambda: i)
lambdas = [build_closure(i) for i in range(10)]
print("%s" % [f() for f in lambdas])
See http://stackoverflow.com/questions/233673/lexical-closures-i...You can write something close only with lambda this way:
lambdas = [(lambda j=i: j) for i in range(10)]
The behavior is actually consistent between generators and list comprehensions, but it's giving the same result as a closure because generators are lazily evaluated (so f() is evaluated right on time, but i is still not enclosed), and work only once: running it twice will have the generator exhausted: In [12]: lambdas_gen = ((lambda: i) for i in xrange(10))
In [13]: [f() for f in lambdas_gen]
Out[13]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [14]: [f() for f in lambdas_gen]
Out[14]: []
That's why forcing the generation with list() causes evaluation of the generator, and only then do you evaluate the f()s, hence the same result as the list comprehension case.Again, changing it to the following properly encloses i:
lambdas_genlist = list((lambda j=i: j) for i in range(10))Well, they should be closures, or they shouldn't be there.
This isn't a question of not understanding Python's semantic rules, it's a question of those rules being screwed. I understand why it's not consistent with generators (as you say - i isn't generated yet). I don't understand why it's not consistent with what you'd expect, namely lambdas not being closures.
It's an even weirder gotcha than:
def f(x = []):
x.append(1)
return x
and we know how many people get hit with that one ;)How closures work depend wildly on the language. With lexical closures it all comes down to how scopes are handled [0] and how and when variable binding is done [1] (notably §8). The fact that 'i' can be either bound late (giving the 'outer scope' effect) or bound early (giving the 'inner scope closure' you expect) is actually a quite useful feature (and I assure you both cases are equally useful), although admittedly a bit surprising when coming from other languages.
Default argument value evaluation is a nice gotcha, but it's a trade-off I'm more than willing to accept [2].
Anyway I would definitely not qualify this as 'screwed'.
[0] http://stackoverflow.com/a/292502/368409
In [1]: lambdas_listgen = list(((lambda: i) for i in range(10)))
In [2]: [f() for f in lambdas_listgen]
Out[2]: [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]While it's possible to write this in Python:
for i in [1, 2, 3, 4]:
pass
final_value = i
The same in C# is not possible, i.e.: int[] values = {1, 2, 3, 4};
foreach(int i in values) {
}
var final_value = i; // there is no i here!
It makes no sense to "fix" this in Python because the loop variable is created in the scope outside of the for loop. It seems to make sense in the case of the C# foreach (but not the C# for!) because that variable is inaccessible outside of the foreach loop scope anyway. I would still argue introducing inconsistent behaviour between for and foreach as they are doing in C# 5 is just going to further obscure this problem and not really eliminate it.Anyway, as far as Python is concerned, closures close over variables not values. Creating special cases where this is not the case is bound to generate even greater confusion.
Here's another example:
in: lambdas = [(lambda i: lambda: i)(i) for i in xrange(10)]
in: [f() for f in lambdas]
out: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Why is it a different i? It's misleading because sometime we have a reference and sometime we've got a whole new variable. map(lambda i: (lambda: i), xrange(10))
but apparently it doesn't.I believe it doesn't desugar to a map+lambda for performance reasons.