Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99029

Question about 'print' in a loop

Newsgroups comp.lang.python
Date 2015-11-18 18:55 -0800
Message-ID <75f51ee1-907c-47c4-9be9-58241ff83115@googlegroups.com> (permalink)
Subject Question about 'print' in a loop
From fl <rxjwg98@gmail.com>

Show all headers | View raw


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,

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Question about 'print' in a loop fl <rxjwg98@gmail.com> - 2015-11-18 18:55 -0800
  Re: Question about 'print' in a loop Chris Angelico <rosuav@gmail.com> - 2015-11-19 14:11 +1100
    Re: Question about 'print' in a loop fl <rxjwg98@gmail.com> - 2015-11-18 19:20 -0800
  Re: Question about 'print' in a loop dieter <dieter@handshake.de> - 2015-11-19 08:31 +0100

csiph-web