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

Barry Warsaw barry at python.org
Thu Apr 25 21:30:19 CEST 2013


On Apr 25, 2013, at 11:42 AM, Antoine Pitrou wrote:

>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]>

No, this is a failed experiment.  It's deprecated in flufl.enum and shouldn't
make it into stdlib enum.  getitem syntax is TOOWTDI.

>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)
>    >>> 

class Methods(Enum):
    set = 1
    get = 2
    delete = 3

What is "Methods.get"?

Catching the ValueError ``Methods[5]`` would raise doesn't seem so bad.

-Barry


More information about the Python-Dev mailing list