Path: csiph.com!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '"""': 0.05; '*not*': 0.07; 'cc:addr:python-list': 0.09; 'foo,': 0.09; 'incidental': 0.09; '\xe2\x80\x94': 0.09; 'python': 0.10; 'wed,': 0.15; 'explicitly': 0.15; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'lambda': 0.16; 'py3': 0.16; 'subject:class': 0.16; 'wrote:': 0.16; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; 'work,': 0.21; 'function:': 0.22; 'code,': 0.23; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'does,': 0.29; 'code': 0.30; 'run': 0.33; 'class': 0.33; 'wrap': 0.33; 'correctly': 0.34; 'definition': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'but': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; '12,': 0.37; 'names': 0.38; 'protection': 0.60; 'beautiful': 0.66; 'worth': 0.67; 'apart': 0.70; 'skip:\xe2 10': 0.70; "'foo'": 0.84; 'bar)': 0.84; 'chrisa': 0.84; 'hardly': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type:content-transfer-encoding; bh=rkpxoUVjwtFDAv3pyGKv6Bkn5k5TE7OY7zDPYdlWhOU=; b=jqZ+2sZW+iqtA+DbIksMEEQLDTQoi7y6ADX1QOzyascTQNetfDX/09dKHxmVryz+bC nQat7VutHrhPnlqrkzU1VCAonlVSLQNid8uHUO9bg4ck2AZuVJs6ac1BjaGTSjCocQ2+ VnaLgqjEV5JIGvjSi2UpKt3y7Rh03/Mc0Sd7EErLtneuOEBUdHTufctC79Pw+jsIcCoh Gqg2HziIsctChjIbhDtTP9SQepYPWKVHap+aSYjqeKLaAdmiXF2gcOeAYy7vaxDBLgnY i5zHfboy2rdI5I9if/LBpTHDPfdjyPJBzLQUJnBpHn5TTgwli88pVDOve0if7rkQ2UtN EXaA== MIME-Version: 1.0 X-Received: by 10.107.31.134 with SMTP id f128mr30200545iof.19.1439370870205; Wed, 12 Aug 2015 02:14:30 -0700 (PDT) In-Reply-To: <85fv3oj2y6.fsf@benfinney.id.au> References: <85fv3oj2y6.fsf@benfinney.id.au> Date: Wed, 12 Aug 2015 19:14:30 +1000 Subject: Re: Ensure unwanted names removed in class definition From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1439370878 news.xs4all.nl 2927 [2001:888:2000:d::a6]:32819 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95276 On Wed, Aug 12, 2015 at 7:01 PM, Ben Finney wr= ote: > class Parrot: > """ A parrot with beautiful plumage. """ > > plumage =3D [ > (foo, bar) for (foo, bar) in feathers.items() > if bar =3D=3D "beautiful"] > del foo, bar # =E2=86=90 FAILS, =E2=80=9CNameError: name 'foo' i= s not defined=E2=80=9D > > How can I write the class definition with the list comprehension and > *not* keep the incidental names =E2=80=94 in code that will run correctly= on > both Python 2 and Python 3? You could always do explicitly what a Py3 comprehension does, and wrap it in a function: plumage =3D (lambda: [ (foo, bar) for (foo, bar) in feathers.items() if bar =3D=3D "beautiful"])() Hardly clean code, but it will work, and apart from having a redundant layer of protection in Py3, will do exactly the same thing on both. Is the lambda nesting cruft worth it? ChrisA