[docs] [issue17947] Code, test, and doc review for PEP-0435 Enum

Zachary Ware report at bugs.python.org
Mon May 13 22:17:26 CEST 2013

Zachary Ware added the comment:

I've come across something in the implementation here that I'd like some clarification on.  What is the purpose of overriding __dir__ in Enum and EnumMeta?  It doesn't change any behavior that I'm aware of, just makes things look a little nicer when someone calls dir() on their Enum.  And, in fact, it can make things a little confusing.  For example:

>>> class Test(enum.Enum):
...     foo = 1
...     bar = 2
...     baz = 3
>>> dir(Test)
['__class__', '__doc__', '__members__', 'bar', 'baz', 'foo']
>>> Test.mro
<built-in method mro of EnumMeta object at 0x01D94D20>

This brings up another interesting case:

>>> class Test2(enum.Enum):
...     mro = 1
...     _create = 2
>>> dir(Test2)
['__class__', '__doc__', '__members__', '_create', 'mro']
>>> Test2.__members__
mappingproxy(OrderedDict([('mro', <Test2.mro: 1>), ('_create', <Test2._create: 2>)]))
>>> Test2['mro']
<Test2.mro: 1>
>>> Test2.mro
<built-in method mro of EnumMeta object at 0x01D90210>
>>> Test2._create
<bound method type._create of <class 'enum.EnumMeta'>>

>From using "mro" or "_create", I would have expected either ValueError or for them to work properly.  I don't know whether this should be fixed (one way or the other), documented, or just left alone; those kind of names really shouldn't ever be used anyway.  It's something I stumbled across, though, and I just wanted to make sure that those who do have opinions that matter are aware of it :)


Python tracker <report at bugs.python.org>

More information about the docs mailing list