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


Groups > comp.lang.python > #16675

Re: Scope of variable inside list comprehensions?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'way:': 0.05; 'exception,': 0.07; 'raises': 0.07; 'python': 0.08; 'exception.': 0.09; 'ids': 0.09; 'nameerror:': 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; 'exception': 0.12; 'def': 0.13; '"unknown': 0.16; '__init__': 0.16; 'docs,': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'roy': 0.16; 'subject:variable': 0.16; 'url:datastructures': 0.16; 'url:html)': 0.16; 'wrote:': 0.18; 'subject:list': 0.21; '(most': 0.21; 'changed': 0.23; 'from:addr:web.de': 0.23; 'defined': 0.24; 'traceback': 0.24; 'says': 0.25; 'skip:_ 20': 0.26; 'django': 0.26; 'raise': 0.28; 'pass': 0.29; 'print': 0.29; 'class': 0.29; 'handling': 0.30; 'python3': 0.30; 'subject:?': 0.31; 'does': 0.32; 'list': 0.32; 'rather': 0.33; 'header:X-Complaints-To:1': 0.33; 'to:addr:python-list': 0.34; "we're": 0.34; 'last):': 0.34; 'try:': 0.34; 'url:python': 0.36; 'file': 0.36; 'thread': 0.37; 'url:pipermail': 0.37; 'skip:" 10': 0.37; 'but': 0.37; 'except': 0.37; 'another': 0.37; 'skip:_ 10': 0.37; 'using': 0.38; 'received:org': 0.38; 'url:docs': 0.39; 'url:org': 0.39; 'to:addr:python.org': 0.40; 'might': 0.40; '11,': 0.68; 'song': 0.73; 'future,': 0.76; 'guaranteed': 0.77; 'song:': 0.84; 'url :python-bugs-list': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Scope of variable inside list comprehensions?
Date Mon, 05 Dec 2011 19:10:08 +0100
Organization None
References <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084b382.dip.t-dialin.net
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.3312.1323108625.27778.python-list@python.org> (permalink)
Lines 81
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1323108625 news.xs4all.nl 6858 [2001:888:2000:d::a6]:36593
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:16675

Show key headers only | View raw


Roy Smith wrote:

> Consider the following django snippet.  Song(id) raises DoesNotExist if
> the id is unknown.
> 
>     try:
>         songs = [Song(id) for id in song_ids]
>     except Song.DoesNotExist:
>         print "unknown song id (%d)" % id
> 
> Is id guaranteed to be in scope in the print statement?  I found one
> thread
> (http://mail.python.org/pipermail/python-bugs-list/2006-April/033235.html)
> which says yes, but hints that it might not always be in the future.  Now
> that we're in the future, is that still true?  And for Python 3 also?
> 
> The current docs,
> http://docs.python.org/tutorial/datastructures.html#list-comprehensions,
> are mute on this point.

If you are using a generator expression id will already be out of scope in 
Python 2. In Python 3 list comprehensions have been changed to work the same 
way:

$ cat song.py
class DoesNotExist(Exception):
    pass

class Song:
    def __init__(self, id):
        if id == 2:
            raise DoesNotExist

ids = [1, 2]
try:
    songs = [Song(i) for i in ids]
except DoesNotExist as e:
    print("song #%d does not exist" % i)
$ python song.py
song #2 does not exist
$ python3 song.py
Traceback (most recent call last):
  File "song.py", line 11, in <module>
    songs = [Song(i) for i in ids]
  File "song.py", line 11, in <listcomp>
    songs = [Song(i) for i in ids]
  File "song.py", line 7, in __init__
    raise DoesNotExist
__main__.DoesNotExist

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "song.py", line 13, in <module>
    print("song #%d does not exist" % i)
NameError: name 'i' is not defined
$


$ cat song_gen.py
class DoesNotExist(Exception):
    pass

class Song:
    def __init__(self, id):
        if id == 2:
            raise DoesNotExist

ids = [1, 2]
try:
    songs = list(Song(i) for i in ids)
except DoesNotExist as e:
    print("song #%d does not exist" % i)
$ python  song_gen.py
Traceback (most recent call last):
  File "song_gen.py", line 13, in <module>
    print("song #%d does not exist" % i)
NameError: name 'i' is not defined

So you'd rather store the id in the exception.

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