[Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library

Michael Urman murman at gmail.com
Fri Apr 12 16:03:48 CEST 2013


> You may not define two enumeration values with the same integer value::
>
>     >>> class Bad(Enum):
>     ...     cartman = 1
>     ...     stan = 2
>     ...     kyle = 3
>     ...     kenny = 3 # Oops!
>     ...     butters = 4
>     Traceback (most recent call last):
>     ...
>     ValueError: Conflicting enums with value '3': 'kenny' and 'kyle'
>
> You also may not duplicate values in derived enumerations::
>
>     >>> class BadColors(Colors):
>     ...     yellow = 4
>     ...     chartreuse = 2 # Oops!
>     Traceback (most recent call last):
>     ...
>     ValueError: Conflicting enums with value '2': 'green' and 'chartreuse'
>

Is there a convenient way to change this behavior, namely to indicate that
conflicts are acceptable in a given Enum? While I like the idea of catching
mistaken collisions, I've seen far too many C/C++ scenarios where multiple
names map to the same value. This does raise questions, as it's unclear
whether each name should get its own representation, or whether it's better
to let DupEnum.name1 is DupEnum.name2 be True.

(For the latter behavior, would adding DupEnum.name2 = DupEnum.name1 after
the class declaration work today?)

Michael
-- 
Michael Urman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130412/36fdbe4f/attachment.html>


More information about the Python-Dev mailing list