Twisted (or for loops ?) madness

Michele Simionato michele.simionato at gmail.com
Mon Oct 15 12:33:17 CEST 2007


On Oct 15, 12:10 pm, looping <kad... at gmail.com> wrote:
>
> Why lambda doesn't work ? (variable scope problem ?)

This is a well known issue of for loops. Some believe it to be a bug
but Guido says it
is a design decision, in the sense that Python always do late binding.
If you
browse this list you will find many discussions. Basically if you do

funclist = []
for i in 1,2,3:
  def f():
     print i
  funclist.append(f)

you will get funclist[0]() == funclist[1]() == funclist[2]() == 3 (you
get the latest
binding of "i"). As you see, it has nothing to do with lambdas.


   Michele Simionato




More information about the Python-list mailing list