fl <rxjw...@gmail.com> writes: > Hi, > > From previous post, I get many helpful replies. Now I have a new question > when I run this example code: > > > --------- > sq=[] > for xx in range(5): > print 'here' > sq.append(lambda:xx**2) > ... > sq[2]() > Out[151]: 16 > > sq[3]() > Out[152]: 16 > /////
Same reason as in your previous thread: variables in (the body of) function definitions (and "lambda"s, which are functions definitions, too) are resolved/dereferenced at call time, not at function definition time. -- https://mail.python.org/mailman/listinfo/python-list