[issue22505] Expose an Enum object's serial number
Barry A. Warsaw
report at bugs.python.org
Sat Sep 27 18:46:11 CEST 2014
Barry A. Warsaw added the comment:
On Sep 27, 2014, at 04:15 PM, Ram Rachum wrote:
>The main principle is: If something has an important property (in this case
>an enum object's numerical value), it should be publicly exposed.
I think this is a misunderstanding. Only IntEnum members have a defined
numerical value. Base Enum members have no inherent value semantics except
their existence. The *syntax* of using integers for values is simply a
convention and one that's not even necessary for Enums to work properly.
Enum members are also defined to be unordered, so their serial number is
meaningless. The fact that __members__ is an ordered dictionary is a
convenient implementation detail that's only exposed in the API to support
iteration over all members including aliases.
Let me say specifically that I am opposed to int() for coercion for
non-IntEnums because Enum values can be anything. Likewise, member.number is
also a misnomer in this case:
>>> from enum import Enum
>>> class Colors(Enum):
... a = 'a'
... b = 'b'
... c = 'c'
...
>>> Colors.a is Colors.b
False
>>> Colors.a is Colors.a
True
I think using IntEnums or a subclass to provide a convenient wrapper around
__members__ iteration is the only thing that makes sense here, but I still
don't think the stdlib needs to support it. IMHO, this is a case where adding
to the public stdlib API would provide more complexity for little common
benefit.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue22505>
_______________________________________
More information about the Python-bugs-list
mailing list