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: 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> <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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Tue, Dec 6, 2011 at 9:57 AM, Roy Smith wrote: > I may be in the minority here, but it doesn't bother me much that my use = of 'id' shadows a built-in. =A0Especially in small scopes like this, I use = whatever variable names make the the code easiest to read and don't worry a= bout shadowing builtins. =A0I 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. =A0So I don't sweat it. > > If shadowing builtins was really evil, they'd be reserved keywords and th= en 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 =3D request.POST.getlist('song_id') songs =3D [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