Path: csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!bcyclone01.am1.xlned.com!bcyclone01.am1.xlned.com!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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'elegant,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'unexpected': 0.09; 'python': 0.10; 'explicitly': 0.15; "'c'": 0.16; 'expression.': 0.16; 'expressions,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:class': 0.16; 'attribute': 0.18; 'documented': 0.18; '>>>': 0.20; '"",': 0.22; "world's": 0.22; '(most': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'then.': 0.27; 'behaviour': 0.29; 'probably': 0.31; 'class': 0.33; 'traceback': 0.33; 'file': 0.34; 'list': 0.34; 'but': 0.36; "wasn't": 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'turned': 0.38; 'speak': 0.38; 'to:addr:python.org': 0.40; 'per': 0.62; 'more': 0.63; 'between': 0.65; 'god': 0.67; '_o__)': 0.84; 'otten': 0.84; 'received:125': 0.84; 'leak': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Re: Ensure unwanted names removed in class definition Date: Thu, 13 Aug 2015 08:50:52 +1000 References: <85fv3oj2y6.fsf@benfinney.id.au> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) Cancel-Lock: sha1:vnYA21r1y9AHOtIobkgZ0Ga0Bkk= 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: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1439419869 news.xs4all.nl 2871 [2001:888:2000:d::a6]:48012 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 3972 X-Received-Body-CRC: 3435084235 Xref: csiph.com comp.lang.python:95323 Peter Otten <__peter__@web.de> writes: > I would probably use a generator expression. These don't leak names: That's an unexpected inconsistency between list comprehensions versus generator expressions, then. Is that documented explicitly in the Python 2 documentation? > Python 2.7.6 (default, Jun 22 2015, 17:58:13) > [GCC 4.8.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> class Parrot: > ... a = [per for per in "abc"] > ... b = list(trans for trans in "def") > ... > >>> Parrot.per > 'c' > >>> Parrot.trans > Traceback (most recent call last): > File "", line 1, in > AttributeError: class Parrot has no attribute 'trans' We have a winner! That's elegant, expressive, and has the right behaviour on both Python 2 and Python 3. -- \ “I turned to speak to God/About the world's despair; But to | `\ make bad matters worse/I found God wasn't there.” —Robert Frost | _o__) | Ben Finney