Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99029 > unrolled thread
| Started by | fl <rxjwg98@gmail.com> |
|---|---|
| First post | 2015-11-18 18:55 -0800 |
| Last post | 2015-11-19 08:31 +0100 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | fl <rxjwg98@gmail.com> |
|---|---|
| Date | 2015-11-18 18:55 -0800 |
| Subject | Question about 'print' in a loop |
| Message-ID | <75f51ee1-907c-47c4-9be9-58241ff83115@googlegroups.com> |
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,
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-11-19 14:11 +1100 |
| Message-ID | <mailman.444.1447902665.16136.python-list@python.org> |
| In reply to | #99029 |
On Thu, Nov 19, 2015 at 1:55 PM, fl <rxjwg98@gmail.com> wrote: > 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? Code does what code should. Before you ask for comprehension of "this phenomenon", why don't you tell us what you expect your code to do, and why? I just asked my non-programmer sister and she was completely unsurprised by what Python did here. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | fl <rxjwg98@gmail.com> |
|---|---|
| Date | 2015-11-18 19:20 -0800 |
| Message-ID | <9982d291-891a-4132-a0cf-2bc94b50d05f@googlegroups.com> |
| In reply to | #99030 |
On Wednesday, November 18, 2015 at 10:11:24 PM UTC-5, Chris Angelico wrote: > On Thu, Nov 19, 2015 at 1:55 PM, fl wrote: > > 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? > > Code does what code should. > > Before you ask for comprehension of "this phenomenon", why don't you > tell us what you expect your code to do, and why? I just asked my > non-programmer sister and she was completely unsurprised by what > Python did here. > > ChrisA Excuse me. That is my unclear question. I expect that each call sq will have one print 'here', as I suppose that print 'here' is inside the loop. Oh, I just realize that it is not a function. It constructs list sq. When the loop (function as a constructor (not necessarily correct called), it print 5 'here'. Thanks for you feedback.
[toc] | [prev] | [next] | [standalone]
| From | dieter <dieter@handshake.de> |
|---|---|
| Date | 2015-11-19 08:31 +0100 |
| Message-ID | <mailman.446.1447918506.16136.python-list@python.org> |
| In reply to | #99029 |
fl <rxjwg98@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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web