Question about 'print' in a loop

fl rxjwg98 at gmail.com
Wed Nov 18 21:55:33 EST 2015


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)
    
here
here
here
here
here

xx
Out[150]: 4

sq[2]()
Out[151]: 16

sq[3]()
Out[152]: 16
/////

There are only one time 5 'here' printed out, but there is no 'here' print
out in thereafter call sq[2]() etc. How to understand this phenomenon?

Thanks,




More information about the Python-list mailing list