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


Groups > comp.lang.python > #16684

Re: Scope of variable inside list comprehensions?

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Scope of variable inside list comprehensions?
Date 2011-12-05 15:22 -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>
Newsgroups comp.lang.python
Message-ID <mailman.3319.1323116550.27778.python-list@python.org> (permalink)

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