Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'raises': 0.07; 'python': 0.08; '%s"': 0.09; 'builtin': 0.09; 'exceptions': 0.09; 'to:addr:comp.lang.python': 0.09; 'exception': 0.12; 'def': 0.13; '"unknown': 0.16; 'docs,': 0.16; 'propagate': 0.16; 'received:192.168.200': 0.16; 'roy': 0.16; 'subject:variable': 0.16; 'url:datastructures': 0.16; 'url:html)': 0.16; 'cc:addr :python-list': 0.16; 'meant': 0.17; 'wrote:': 0.18; 'cc:no real name:2**0': 0.20; 'subject:list': 0.21; 'header:In-Reply-To:1': 0.22; 'defined': 0.24; 'cc:2**0': 0.24; 'says': 0.25; 'django': 0.26; 'function': 0.27; 'raise': 0.28; 'print': 0.29; 'cc:addr:python.org': 0.29; 'class': 0.29; 'correct': 0.29; 'subject:?': 0.31; 'header:User-Agent:1': 0.33; 'probably': 0.34; "we're": 0.34; 'function.': 0.34; 'try:': 0.34; 'something': 0.35; 'url:python': 0.36; 'thread': 0.37; 'url:pipermail': 0.37; 'but': 0.37; 'except': 0.37; 'received:192': 0.37; 'skip:_ 10': 0.37; 'doing': 0.38; 'using': 0.38; 'url:docs': 0.39; 'url:org': 0.39; 'might': 0.40; 'received:192.168': 0.40; 'header:Received:6': 0.61; 'received:62': 0.70; 'song': 0.73; 'future,': 0.76; 'guaranteed': 0.77; 'exc:': 0.84; 'song:': 0.84; 'url:python-bugs- list': 0.84; 'achieve.': 0.96 X-IronPort-AV: E=Sophos;i="4.71,300,1320620400"; d="scan'208";a="27726" X-Virus-Scanned: amavisd-new at zimbra.sequans.com Date: Mon, 05 Dec 2011 19:57:15 +0100 From: Jean-Michel Pichavant User-Agent: Mozilla-Thunderbird 2.0.0.24 (X11/20100328) MIME-Version: 1.0 To: comp.lang.python@googlegroups.com Subject: Re: Scope of variable inside list comprehensions? References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> In-Reply-To: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323111443 news.xs4all.nl 6937 [2001:888:2000:d::a6]:34737 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16679 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