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


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

Ensure unwanted names removed in class definition

Started byBen Finney <ben+python@benfinney.id.au>
First post2015-08-12 19:01 +1000
Last post2015-08-12 19:33 +0300
Articles 2 — 2 participants

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


Contents

  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

#95274 — Ensure unwanted names removed in class definition

FromBen Finney <ben+python@benfinney.id.au>
Date2015-08-12 19:01 +1000
SubjectEnsure unwanted names removed in class definition
Message-ID<mailman.102.1439370080.3627.python-list@python.org>
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

[toc] | [next] | [standalone]


#95294

FromJussi Piitulainen <ei@kun.ei.invalid>
Date2015-08-12 19:33 +0300
Message-ID<lf5si7otqkj.fsf@ling.helsinki.fi>
In reply to#95274
Ben Finney writes:

> 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
[- -]
> 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?

Make them an implementation detail:

    plumage = [
        (__foo__, __bar__) for (__foo__, __bar__) in feathers.items()
        if __bar__ == "beautiful" ]

[toc] | [prev] | [standalone]


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


csiph-web