Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #95274
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.004 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; '"""': 0.05; '*not*': 0.07; 'definition,': 0.09; 'foo,': 0.09; 'incidental': 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; "'foo')": 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:class': 0.16; 'stick': 0.18; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; "i'm": 0.30; 'code': 0.30; 'skip:_ 10': 0.32; 'run': 0.33; 'class': 0.33; 'correctly': 0.34; 'definition': 0.34; 'list': 0.34; 'so,': 0.35; 'but': 0.36; 'needed': 0.36; 'to:addr :python-list': 0.36; 'received:org': 0.37; 'wanted': 0.37; 'names': 0.38; 'end': 0.39; 'to:addr:python.org': 0.40; 'beautiful': 0.66; 'skip:\xe2 10': 0.70; "'foo'": 0.84; '_o__)': 0.84; 'bar)': 0.84; 'received:125': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Ben Finney <ben+python@benfinney.id.au> |
| Subject | Ensure unwanted names removed in class definition |
| Date | Wed, 12 Aug 2015 19:01:05 +1000 |
| 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 <bignose+hates-spam@benfinney.id.au> |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) |
| Cancel-Lock | sha1:nOAB7zZ3F5g+UHjCW/2Xfm7aFT8= |
| 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.102.1439370080.3627.python-list@python.org> (permalink) |
| Lines | 59 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1439370080 news.xs4all.nl 2901 [2001:888:2000:d::a6]:57272 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:95274 |
Show key headers only | View raw
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?
--
\ “Pinky, are you pondering what I'm pondering?” “Well, I think |
`\ so, but *where* do you stick the feather and call it macaroni?” |
_o__) —_Pinky and The Brain_ |
Ben Finney
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Ensure unwanted names removed in class definition Ben Finney <ben+python@benfinney.id.au> - 2015-08-12 19:01 +1000 Re: Ensure unwanted names removed in class definition Jussi Piitulainen <ei@kun.ei.invalid> - 2015-08-12 19:33 +0300
csiph-web