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


Groups > comp.lang.python > #16697

Re: Scope of variable inside list comprehensions?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!news-transit.tcx.org.uk!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'context': 0.04; 'builtins': 0.07; 'programmer': 0.10; 'am,': 0.12; 'exception': 0.12; 'def': 0.13; 'received:209.85.210.174': 0.13; 'received :mail-iy0-f174.google.com': 0.13; 'builtins.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'head,': 0.16; 'infinitely': 0.16; 'occasionally': 0.16; 'roy': 0.16; 'scopes': 0.16; 'subject:variable': 0.16; "there'll": 0.16; 'worried': 0.16; "wouldn't": 0.17; 'wrote:': 0.18; '\xa0so': 0.18; 'subject:list': 0.21; 'dec': 0.22; "doesn't": 0.22; 'header:In- Reply-To:1': 0.22; "python's": 0.24; 'code': 0.25; 'code.': 0.26; "i'm": 0.26; 'skip:[ 10': 0.27; 'variable': 0.28; 'putting': 0.28; 'bit': 0.28; 'message-id:@mail.gmail.com': 0.28; 'explicit': 0.29; 'weird': 0.29; 'agreed.': 0.30; 'subject:?': 0.31; 'cases': 0.32; 'list': 0.32; 'tue,': 0.32; 'rules': 0.34; 'done': 0.34; 'to:addr :python-list': 0.34; 'probably': 0.34; 'it.': 0.34; 'filter': 0.34; 'question,': 0.34; 'things': 0.34; 'begins': 0.34; 'function.': 0.34; 'nested': 0.34; 'try:': 0.34; 'here,': 0.35; 'issue': 0.35; 'file': 0.36; 'minority': 0.37; 'but': 0.37; 'except': 0.37; 'received:google.com': 0.37; "there's": 0.37; 'could': 0.37; 'easiest': 0.38; 'somewhat': 0.38; 'some': 0.38; 'received:209.85': 0.38; 'flexibility': 0.39; "i'd": 0.39; '(with': 0.39; "it's": 0.40; 'received:209': 0.40; 'to:addr:python.org': 0.40; 'more': 0.61; '2011': 0.61; 'worth': 0.61; 'miss': 0.62; '"do': 0.67; 'song': 0.73; '"just': 0.84; 'encapsulates': 0.84; 'evil,': 0.84; 'confusion.': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=Z/QYINH7Wb2VbaQEQ7zxUJancVkApIXqIMmuIf4PA7E=; b=DshUUCJnZux6g9KVSCURY63h/+NEcDDpw5GyD+pyO+iFAY3dq4Vuu0VFYf0YDpjf1C dDVkBRYdYQP0J3+5f2a4JH+jxbnGs84YtbzgmzBI4rcuD/aQ7caELykhfv0lwJbMh8f6 ERnnVJErM9QQV0Al/UypRzAuEUX5jxHlRL7A0=
MIME-Version 1.0
In-Reply-To <14157454.1816.1323125863703.JavaMail.geo-discussion-forums@yqbz41>
References <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> <mailman.3315.1323111443.27778.python-list@python.org> <4edd471a$0$29988$c3e8da3$5496439d@news.astraweb.com> <14157454.1816.1323125863703.JavaMail.geo-discussion-forums@yqbz41>
Date Tue, 6 Dec 2011 13:35:45 +1100
Subject Re: Scope of variable inside list comprehensions?
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
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.3329.1323138948.27778.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1323138948 news.xs4all.nl 6987 [2001:888:2000:d::a6]:59407
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:16697

Show key headers only | View raw


On Tue, Dec 6, 2011 at 9:57 AM, Roy Smith <roy@panix.com> wrote:
> I may be in the minority here, but it doesn't bother me much that my use of 'id' shadows a built-in.  Especially in small scopes like this, I use whatever variable names make the the code easiest to read and don't worry about shadowing builtins.  I don't have an exhaustive list of builtins in my head, so even if I worried about common ones like file or id, I'm sure I'd miss some others.  So I don't sweat it.
>
> If shadowing builtins was really evil, they'd be reserved keywords and then you wouldn't be able to do it.

Agreed. The name 'id' is one that's shadowed benignly in a lot of
code. In the context of the original question, it's not an issue -
there'll be no confusion.

Python's scoping rules are "do what the programmer probably wants".
This works most of the time, but occasionally you get edge cases where
things are a bit weird, and C-style explicit scoping (with infinitely
nested block scope) begins to look better. But for probably 99% of
situations, Python's system "just works".

If you need more flexibility in the exception you throw, it may be
worth putting together a filter:

def HttpSongId(id):
   try:
       return Song.get(int(id))
   except Song.DoesNotExist:
       return HttpResponseBadRequest("unknown song id (%d)" % id)

   song_ids = request.POST.getlist('song_id')
   songs = [HttpSongId(id) for id in song_ids]

This encapsulates things in a somewhat weird way, but if there's any
other work to be done at the same time, you could probably come up
with a better name for the exception filter function.

ChrisA

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