On 3/16/21 11:43 AM, Matt Wozniski wrote:
On Tue, Mar 16, 2021, 2:39 PM Marco Sulla wrote:
On Tue, 16 Mar 2021 at 05:38, Matt Wozniski wrote:
Color.from_value(1) # returns Color.RED
What if I have an alias?
Aliases are different names for a single Enum member, so a by-value search is
unaffected by them.
That's a problem with any attempt to find an enum member by value,
since values aren't guaranteed to be unique. With either proposal,
we'd just need to pick one - probably the one that appears first
in the class dict.
This is incorrect. Enum values are unique -- there is only one member that
will be returned for any given value. Aliases are additional names for that
one member:
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
REDD = 1 # to support a silly misspelling
>>> Color.RED
<Color.RED: 1>
>>> Color(1)
<Color.RED: 1>
>>> Color['RED']
<Color.RED: 1>
>>> Color['REDD']
<Color.RED: 1>
Notice that 'REDD' returns the RED member. There is no member named REDD in
Color.
--
~Ethan~
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/XBGO35VFGC7CQLIZJTKH6EAXEKCJJQRC/
Code of Conduct: http://python.org/psf/codeofconduct/