Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #16684
| 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!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; 'django.': 0.05; 'exception,': 0.07; 'posting.': 0.07; 'received:verizon.net': 0.07; 'terry': 0.07; 'python': 0.08; 'introduces': 0.09; 'loop.': 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; 'sake': 0.09; 'storing': 0.09; ';-)': 0.12; 'exception': 0.12; 'done),': 0.16; 'exception;': 0.16; 'hmmm,': 0.16; 'reedy': 0.16; 'roy': 0.16; 'subject:variable': 0.16; 'try/except': 0.16; 'wrote:': 0.18; 'jan': 0.19; 'subject:list': 0.21; 'appropriate': 0.22; 'header:In-Reply-To:1': 0.22; 'along.': 0.23; 'loop,': 0.23; 'skip:{ 20': 0.23; 'code': 0.25; 'django': 0.26; "i'm": 0.26; 'variable': 0.28; '(and': 0.28; 'bit': 0.28; 'explicit': 0.29; "skip:' 10": 0.29; 'problem': 0.29; 'pm,': 0.29; 'subject:?': 0.31; 'changes': 0.32; 'header:User-Agent:1': 0.33; 'header:X -Complaints-To:1': 0.33; 'instead': 0.33; 'to:addr:python-list': 0.34; 'it.': 0.34; 'loop': 0.34; 'try:': 0.34; 'changing': 0.35; 'question': 0.36; 'but': 0.37; 'except': 0.37; 'using': 0.38; 'received:org': 0.38; 'put': 0.38; 'to:addr:python.org': 0.40; 'within': 0.60; 'more': 0.61; 'song': 0.73; 'guaranteed': 0.77; 'original.': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Terry Reedy <tjreedy@udel.edu> |
| Subject | Re: Scope of variable inside list comprehensions? |
| Date | Mon, 05 Dec 2011 15:22:05 -0500 |
| References | <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> <mailman.3315.1323111443.27778.python-list@python.org> <3206622.1235.1323112504669.JavaMail.geo-discussion-forums@yqiw17> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | pool-74-109-121-73.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 |
| In-Reply-To | <3206622.1235.1323112504669.JavaMail.geo-discussion-forums@yqiw17> |
| 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.3319.1323116550.27778.python-list@python.org> (permalink) |
| Lines | 36 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1323116550 news.xs4all.nl 6846 [2001:888:2000:d::a6]:54772 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:16684 |
Show key headers only | View raw
On 12/5/2011 2:15 PM, Roy Smith wrote:
> Hmmm, the use of id was just a simplification for the sake of
> posting. The real code is a bit more complicated and used a
> different variable name, but that's a good point.
>
> As far as storing the value in the exception, unfortunately,
> DoesNotExist is not my exception; it comes from deep within django.
> I'm just passing it along.
It is hard to sensibly answer a question when the questioner
*significantly* changes the problem in the process of 'simplifying' it.
Both of those are significant changes ;-)
Changing a name to a built-in name is a complexification, not a
simplification, because it introduces new issues that were not in the
original.
Changing the exception from one you do not control to one you apparently
do also changes the appropriate answer. If you do not control the
exception and you want guaranteed access to the loop variable with
Python 3 (and the upgrade of django to work with Python 3 is more or
less done), then use an explicit loop. If you want the loop to continue
after an error, instead of stopping, you can put the try/except within
the loop, instead of without. This possibility is one advantage of using
an explicit loop.
songs = []
for song_id in song_ids:
try:
songs.append(Song(song_id))
except django.error:
print("unknown song id {}".format(song_id))
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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