Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: dieter Newsgroups: comp.lang.python Subject: Re: Question about 'print' in a loop Date: Thu, 19 Nov 2015 08:31:04 +0100 Lines: 25 Message-ID: References: <75f51ee1-907c-47c4-9be9-58241ff83115@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news.uni-berlin.de 1ytUWpsjSIUd45O4ppl3DAmwMVIeP5RMFNcRRujr0n7g== Cancel-Lock: sha1:E3htPDgrrqfc2fLQLAkqIwlcsCE= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '---------': 0.05; 'subject:Question': 0.05; 'definitions': 0.07; "subject:' ": 0.07; 'of)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'variables': 0.15; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; '(the': 0.22; 'header:User- Agent:1': 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'helpful': 0.27; 'question': 0.27; 'function': 0.28; 'code:': 0.29; 'print': 0.30; 'run': 0.33; 'definition': 0.34; 'previous': 0.34; '(and': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'charset:us-ascii': 0.37; 'hi,': 0.38; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; 'body': 0.61; 'received:217': 0.66; "'here'": 0.84; 'post,': 0.84; 'replies.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: pd9e09b8f.dip0.t-ipconnect.de User-Agent: Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99034 fl 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.