Python Mystery Theatre -- Episode 2: Así Fue
vze4rx4y at verizon.net
Tue Jul 15 21:19:56 CEST 2003
> The fix is easy:
> flam = [lambda x, fn=f: fn(x) for f in funcs]
> which creates a new local binding which captures the correct value at
> each iteration. This is the kind of problem which makes me wonder
> whether we ought to re-think about binding of variables for loops.
Hmm, I put too many distractors in this one.
It's not about lambda and loops. And though it touches
on nested scopes and list comprehensions, the crux is
just plain old bound / unbound variables inside a function
>>> base = hex
>>> def changebase(x):
... return base(x)
>>> base = oct
It's a feature!
More information about the Python-list