Scope of variable inside list comprehensions?
Jean-Michel Pichavant
jeanmichel at sequans.com
Mon Dec 5 13:57:15 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.
>
For python 2, id will always be defined *as you meant it*. But you're
doing something wrong : overiding the 'id' builtin function.
With python 3 you will probably print the 'id' builtin function
representation, which is correct but not want you want to achieve.
The proper way to propagate information with exceptions is using the
exception itself:
try:
songs = [Song(_id) for _id in song_ids]
except Song.DoesNotExist, exc:
print exc
class DoesNotExist(Exception):
def __init__(self, songId):
self.songId = songId
def __str__(self):
return "Unkown Song Id %s" % self.songId
class Song:
def __init__(self, songId):
if whatever:
raise DoesNotExist(songId)
self.id=songId
JM
More information about the Python-list
mailing list