Mark H Harris <harrismh...@gmail.com>: > Yes, its about closures, totally; the most confusing aspect of > lambda in python is not only the syntax but the idea of scope and > closure (for that syntax). Everyone is confused by this initially, not > because its complicated, but because its confusing. An example: > >>>>> adders= list(range(4)) >>>>> for n in adders: >> adders[n]=lambda a: a+n >>>>> print(adders[1](3)) >> 6 > > The expected value as perceived by "normal" people is 4.
1. No, I don't think that understanding is automatically natural. 2. It does not concern Python only. For example, what does this scheme expression yield? ((let ((n 3)) (let ((f (lambda () n))) (set! n 7) f))) Answer: 7 3. It doesn't concern lambda only. For example, rewrite your loop like this: for n in range(4): def add(a): return a + n adders[n] = add adders[1](3) => 6 Marko -- https://mail.python.org/mailman/listinfo/python-list