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


Groups > comp.lang.python > #95283

Re: Ensure unwanted names removed in class definition

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 <python@mrabarnett.plus.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; '"""': 0.05; '*not*': 0.07; 'definition,': 0.09; 'foo,': 0.09; 'incidental': 0.09; '\xe2\x80\x94': 0.09; 'python': 0.10; "'foo')": 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'subject:class': 0.16; 'wrote:': 0.16; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'catching': 0.29; 'code': 0.30; 'skip:_ 10': 0.32; 'run': 0.33; 'class': 0.33; 'correctly': 0.34; 'definition': 0.34; 'list': 0.34; 'but': 0.36; 'needed': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'thought': 0.37; 'wanted': 0.37; 'names': 0.38; 'end': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'beautiful': 0.66; 'skip:\xe2 10': 0.70; "'foo'": 0.84; 'bar)': 0.84
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=CvRCCSMD c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=EBOSESyhAAAA:8 a=UWU4mQFGWXkA:10 a=IkcTkHD0fZMA:10 a=OZcEm27X1063dw-i4bAA:9 a=QEXdDO2ut3YA:10
X-AUTH mrabarnett@:2500
Subject Re: Ensure unwanted names removed in class definition
To python-list@python.org
References <85fv3oj2y6.fsf@benfinney.id.au>
From MRAB <python@mrabarnett.plus.com>
Date Wed, 12 Aug 2015 16:14:06 +0100
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0
MIME-Version 1.0
In-Reply-To <85fv3oj2y6.fsf@benfinney.id.au>
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 8bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.110.1439392449.3627.python-list@python.org> (permalink)
Lines 56
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1439392449 news.xs4all.nl 2951 [2001:888:2000:d::a6]:44744
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:95283

Show key headers only | View raw


On 2015-08-12 10:01, 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?
>
Have you thought about catching the NameError?

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Ensure unwanted names removed in class definition MRAB <python@mrabarnett.plus.com> - 2015-08-12 16:14 +0100

csiph-web