[Python-Dev] [Python-checkins] peps: Pre-alpha draft for PEP 435 (enum). The name is not important at the moment, as
Ethan Furman
ethan at stoneleaf.us
Mon Feb 25 20:34:35 CET 2013
Antoine, question for you:
Do you think enums from different groupings should compare equal?
If no, how would you propose to handle the following:
8<----------------------------------------------------------------------------
--> import yaenum
--> class Color(yaenum.Enum):
... black
... red
... green
... blue
...
--> class Literature(yaenum.Enum):
... scifi
... fantasy
... mystery
... pop
...
--> Color.black
Color('black', value=0)
--> Literature.scifi
Literature('scifi', value=0)
--> black = Color.black
--> scifi = Literature.scifi
--> black == 0
True
--> hash(black)
0
--> scifi == 0
True
--> hash(scifi)
0
--> black == scifi
False
--> hash(0)
0
--> huh = dict()
--> huh[black] = 9
--> huh
{Color('black', value=0): 9}
--> huh[0]
9
--> huh[scifi]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: Literature('scifi', value=0)
--> huh[scifi] = 11
--> huh
{Color('black', value=0): 9, Literature('scifi', value=0): 11}
--> huh[0]
9
--> del huh[0]
--> huh[0]
11
8<----------------------------------------------------------------------------
I do not think enums from different classes should compare equal.
I see two ways around the above issue:
1) make enums unhashable, forcing the user to pick a hash method;
2) make the hash based on something else (like the enum's name) in which case
the enum would not be /completely/ interoperable, even though it is a subclass
of int
More information about the Python-Dev
mailing list