On Fri, Feb 22, 2013 at 4:01 PM, Alex Stewart <foogod@gmail.com> wrote:
On Fri, Feb 22, 2013 at 3:45 PM, Alexandre Zani <alexandre.zani@gmail.com> wrote:
On Fri, Feb 22, 2013 at 3:16 PM, Ethan Furman <ethan@stoneleaf.us> wrote:
  * Enum values are hashable, so they can be used as dict keys, etc.

Problem here:  should we have our enums hash the same as the underlying value?  Consider:

One feature of the implementation is that enum values from different enum classes will never equal each other. It would be very weird for them to have identical hashes.

Actually, not really.  The only requirement in Python is that if two objects compare equal, they need to have the same hash value, but there is no requirement the other direction (indeed, that would be a problem in many cases as the domain of hash values is inherently much smaller than the domain of all possible object data, so it must be expected that you will get hash duplication from time to time, no matter what method you use for calculating the hashes).

My bad. Please forget I ever mentioned it.

In the case of pure Enums, they're just singleton object instances, so they hash the same as any other object, which works pretty much as one would expect.

In the case of TypeEnums, since they do compare equal to the underlying value, they must also hash the same. However hashes are just a "hint":  Two objects can hash the same but not compare equal, in which case they will be considered to be different objects when used as dictionary keys, etc.  There is actually an example of this in the examples.py I put up in github with the reference code.  It creates a dictionary with two keys, which are two different Enums, which both use the same int value.  Because they do not compare equal, they are treated as different keys by Python, as I think most people would expect..

--Alex

_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
http://mail.python.org/mailman/listinfo/python-ideas