Thomas Rachel writes: > Am 31.05.2011 12:08 schrieb Jussi Piitulainen: > > > The same sharing-an-i thing happens here: > > > >>>> fs = [] > >>>> for i in range(4): > > ... fs.append(lambda n : i + n) > > ... > >>>> fs[0](0) > > 3 > > > > And the different private-j thing happens here: > > > >>>> gs = [] > >>>> for i in range(4): > > ... gs.append((lambda j : lambda n : j + n)(i)) > > ... > >>>> gs[0](0) > > 0 > > There is a simpler way: with > > >>>> fs = [] > >>>> for i in range(4): > > ... fs.append(lambda n, i=i: i + n) > > ... > > you give each lambda a different default argument. > > >>>> fs[0](0) > > 0
I know, and harrismh777 knows. I find it an unnecessary distraction when explaining why the different closures in the initial example behave identically, but I did discuss it at the end of my post. -- http://mail.python.org/mailman/listinfo/python-list