Scope of variable inside list comprehensions?
Peter Otten
__peter__ at web.de
Mon Dec 5 13:10:08 EST 2011
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.
More information about the Python-list
mailing list