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


Groups > comp.lang.python > #95276 > unrolled thread

Re: Ensure unwanted names removed in class definition

Started byChris Angelico <rosuav@gmail.com>
First post2015-08-12 19:14 +1000
Last post2015-08-12 19:14 +1000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Ensure unwanted names removed in class definition Chris Angelico <rosuav@gmail.com> - 2015-08-12 19:14 +1000

#95276 — Re: Ensure unwanted names removed in class definition

FromChris Angelico <rosuav@gmail.com>
Date2015-08-12 19:14 +1000
SubjectRe: Ensure unwanted names removed in class definition
Message-ID<mailman.103.1439370878.3627.python-list@python.org>
On Wed, Aug 12, 2015 at 7:01 PM, Ben Finney <ben+python@benfinney.id.au> wrote:
>     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?

You could always do explicitly what a Py3 comprehension does, and wrap
it in a function:

plumage = (lambda: [
    (foo, bar) for (foo, bar) in feathers.items()
    if bar == "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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web