Ensure unwanted names removed in class definition

Peter Otten __peter__ at web.de
Wed Aug 12 17:39:41 CEST 2015


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 "<stdin>", line 1, in <module>
AttributeError: class Parrot has no attribute 'trans'




More information about the Python-list mailing list