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


Groups > comp.lang.python > #99034

Re: Question about 'print' in a loop

From dieter <dieter@handshake.de>
Newsgroups comp.lang.python
Subject Re: Question about 'print' in a loop
Date 2015-11-19 08:31 +0100
Message-ID <mailman.446.1447918506.16136.python-list@python.org> (permalink)
References <75f51ee1-907c-47c4-9be9-58241ff83115@googlegroups.com>

Show all headers | View raw


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.

Back to comp.lang.python | Previous | NextPrevious 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