Path: csiph.com!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed7.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"""': 0.05; '*not*': 0.07; 'definition,': 0.09; 'foo,': 0.09; 'incidental': 0.09; 'must:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; '\xe2\x80\x94': 0.09; 'python': 0.10; "'c'": 0.16; "'foo')": 0.16; '...]': 0.16; 'expression.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:class': 0.16; 'wrote:': 0.16; 'attribute': 0.18; '>>>': 0.20; '"",': 0.22; '(most': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'code': 0.30; 'probably': 0.31; 'skip:_ 10': 0.32; 'run': 0.33; 'class': 0.33; 'traceback': 0.33; 'correctly': 0.34; 'definition': 0.34; 'file': 0.34; 'list': 0.34; 'exist': 0.35; 'but': 0.36; 'needed': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'setting': 0.37; 'received:org': 0.37; 'wanted': 0.37; 'names': 0.38; 'end': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'per': 0.62; 'more': 0.63; 'beautiful': 0.66; 'skip:\xe2 10': 0.70; "'foo'": 0.84; 'bar)': 0.84; 'absolutely': 0.88; 'leak': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Ensure unwanted names removed in class definition Date: Wed, 12 Aug 2015 17:39:41 +0200 Organization: None 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: p57bd8aa7.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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: 80 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1439394003 news.xs4all.nl 2931 [2001:888:2000:d::a6]:56094 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95285 Ben Finney wrote: > How can I ensure incidental names don't end up in the class definition, > with code that works on both Python 2 and Python 3? > > With the following class definition, the incidental names `foo` and > `bar`, only needed for the list comprehension, remain in the `Parrot` > namespace:: > > __metaclass__ = object > > class Parrot: > """ A parrot with beautiful plumage. """ > > plumage = [ > (foo, bar) for (foo, bar) in feathers.items() > if bar == "beautiful"] > > assert hasattr(Parrot, 'plumage') # ← okay, has the wanted name > assert not hasattr(Parrot, 'foo') # ← FAILS, has an unwanted name > assert not hasattr(Parrot, 'bar') # ← FAILS, has an unwanted name > > So I can remove those names after using them:: > > __metaclass__ = object > > class Parrot: > """ A parrot with beautiful plumage. """ > > plumage = [ > (foo, bar) for (foo, bar) in feathers.items() > if bar == "beautiful"] > del foo, bar > > assert hasattr(Parrot, 'plumage') # ← okay, has the wanted name > assert not hasattr(Parrot, 'foo') # ← okay, no unwanted name > assert not hasattr(Parrot, 'bar') # ← okay, no unwanted name > > But that fails on Python 3, since the names *don't* persist from the > list comprehension: > > __metaclass__ = object > > class Parrot: > """ A parrot with beautiful plumage. """ > > plumage = [ > (foo, bar) for (foo, bar) in feathers.items() > if bar == "beautiful"] > del foo, bar # ← FAILS, “NameError: name 'foo' is not defined” > > How can I write the class definition with the list comprehension and > *not* keep the incidental names — in code that will run correctly on > both Python 2 and Python 3? If you absolutely must: make sure that the names exist by setting them explicitly: class Parrot: per = None a = [... for per in ...] del per But I would probably use a generator expression. These don't leak names: 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'