[Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library
Eli Bendersky
eliben at gmail.com
Thu Apr 25 14:21:50 CEST 2013
On Thu, Apr 25, 2013 at 2:42 AM, Antoine Pitrou <solipsis at pitrou.net> wrote:
> 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]>
>
>
Yes, this is mentioned in the beginning of the same paragraph.
> or:
>
> >>> Colors(1)
> <EnumValue: Colors.red [value=1]>
>
>
This syntax was considered initially but then rejected because it's
confusing, and there already exists a way to lookup by value (Colors[1]).
Eli
> 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.
>
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/eliben%40gmail.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130425/417c0d8f/attachment.html>
More information about the Python-Dev
mailing list