[Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library (original) (raw)

Antoine Pitrou solipsis at pitrou.net
Thu Apr 25 11:42:57 CEST 2013


Le Fri, 12 Apr 2013 05:55:00 -0700, Eli Bendersky <eliben at gmail.com> a écrit :

To programmatically access enumeration values, use getattr:: >>> getattr(Colors, 'red') <EnumValue: Colors.red [value=1]>

The PEP should mention how to get an enum from its raw value:

>>> Colors[1]
<EnumValue: Colors.red [value=1]>

or:

>>> Colors(1)
<EnumValue: Colors.red [value=1]>

It would perhaps be nice to have a .get() method that return None if the raw value is unknown:

>>> Colors(42)
...
ValueError: 42
>>> Colors.get(42)
>>> 

Regards

Anroine.



More information about the Python-Dev mailing list