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


Groups > comp.lang.python > #16696

Re: Scope of variable inside list comprehensions?

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news-transit.tcx.org.uk!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; ':-)': 0.06; 'exception,': 0.07; 'received:verizon.net': 0.07; 'terry': 0.07; 'admit': 0.09; 'alter': 0.09; 'answering': 0.09; 'builtin': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'narrow': 0.16; 'reedy': 0.16; 'roy': 0.16; 'subject:variable': 0.16; 'wrote:': 0.18; 'jan': 0.19; 'subject:list': 0.21; 'asked': 0.22; 'header:In-Reply-To:1': 0.22; 'knowing': 0.23; 'code': 0.25; 'variable': 0.28; 'random': 0.28; 'print': 0.29; 'pm,': 0.29; '3.x': 0.30; 'subject:?': 0.31; "didn't": 0.31; 'does': 0.32; 'list': 0.32; 'pretty': 0.32; 'header:User-Agent:1': 0.33; 'header:X-Complaints-To:1': 0.33; 'to:addr:python-list': 0.34; 'loop': 0.34; 'question,': 0.34; 'question': 0.36; 'example,': 0.37; 'but': 0.37; 'run': 0.37; 'except': 0.37; 'could': 0.37; 'received:org': 0.38; 'some': 0.38; 'fail': 0.39; 'to:addr:python.org': 0.40; 'questions,': 0.65; 'guaranteed': 0.77; '5:36': 0.84; 'bears': 0.84; 'defense,': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Scope of variable inside list comprehensions?
Date Mon, 05 Dec 2011 21:23:17 -0500
References <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> <mailman.3315.1323111443.27778.python-list@python.org> <3206622.1235.1323112504669.JavaMail.geo-discussion-forums@yqiw17> <mailman.3319.1323116550.27778.python-list@python.org> <27498659.319.1323124608749.JavaMail.geo-discussion-forums@yqcv9>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-74-109-121-73.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0
In-Reply-To <27498659.319.1323124608749.JavaMail.geo-discussion-forums@yqcv9>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3328.1323138219.27778.python-list@python.org> (permalink)
Lines 24
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1323138219 news.xs4all.nl 6925 [2001:888:2000:d::a6]:57005
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:16696

Show key headers only | View raw


On 12/5/2011 5:36 PM, Roy Smith wrote:
> Well, in my defense, I did ask a pretty narrow question, "Is id
> guaranteed to be in scope in the print statement?".

Yes for 2.x, guaranteed no for 3.x.

If you had simply asked "Is the loop variable of a list comprehension 
guaranteed to be in scope after the list comprehension?", without a 
distracting example, that is the answer you would have received.

I intend(ed) to inform, not attack, hence no 'defense' needed.

>  While I will
> admit that not knowing whether I could alter the exception, or
> whether id masked a builtin or not does complexify answering some
> questions, those are questions I didn't ask :-)

Except that it bears on the question you did ask because it means that 
the code will run in 3.x but with different results, whereas a random 
name will fail in 3.x with a NameError.

-- 
Terry Jan Reedy

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


Thread

Scope of variable inside list comprehensions? Roy Smith <roy@panix.com> - 2011-12-05 09:04 -0800
  Re: Scope of variable inside list comprehensions? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2011-12-05 20:04 +0200
  Re: Scope of variable inside list comprehensions? Peter Otten <__peter__@web.de> - 2011-12-05 19:10 +0100
  Re: Scope of variable inside list comprehensions? Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-12-05 19:57 +0100
    Re: Scope of variable inside list comprehensions? Roy Smith <roy@panix.com> - 2011-12-05 11:15 -0800
    Re: Scope of variable inside list comprehensions? Roy Smith <roy@panix.com> - 2011-12-05 11:15 -0800
      Re: Scope of variable inside list comprehensions? Terry Reedy <tjreedy@udel.edu> - 2011-12-05 15:22 -0500
        Re: Scope of variable inside list comprehensions? Roy Smith <roy@panix.com> - 2011-12-05 14:36 -0800
        Re: Scope of variable inside list comprehensions? Roy Smith <roy@panix.com> - 2011-12-05 14:36 -0800
          Re: Scope of variable inside list comprehensions? Terry Reedy <tjreedy@udel.edu> - 2011-12-05 21:23 -0500
    Re: Scope of variable inside list comprehensions? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-05 22:35 +0000
      Re: Scope of variable inside list comprehensions? Roy Smith <roy@panix.com> - 2011-12-05 14:57 -0800
        Re: Scope of variable inside list comprehensions? Chris Angelico <rosuav@gmail.com> - 2011-12-06 13:35 +1100
      Re: Scope of variable inside list comprehensions? Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-12-06 11:38 +0100
  Re: Scope of variable inside list comprehensions? Rainer Grimm <r.grimm@science-computing.de> - 2011-12-05 22:42 -0800
    Re: Scope of variable inside list comprehensions? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-06 02:16 -0800

csiph-web