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
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to