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


Groups > comp.lang.python > #16675

Re: Scope of variable inside list comprehensions?

From Peter Otten <__peter__@web.de>
Subject Re: Scope of variable inside list comprehensions?
Date 2011-12-05 19:10 +0100
Organization None
References <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14>
Newsgroups comp.lang.python
Message-ID <mailman.3312.1323108625.27778.python-list@python.org> (permalink)

Show all headers | 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