可哈希 class 与 Python 中的枚举的交互

Interaction of a hashable class with Enum in Python

我已经定义了一个 class,我试图将其设为可哈希。此外,还有一个枚举,它使用此 class 的对象作为其枚举成员的值。

from enum import Enum


class Dummy(object):
    def __init__(self, name, property_):
        self.name = name             # can only be a string
        self.property = property_    # can only be a string

    def __hash__(self):
        # print "Hash called for ", self
        # print("----")
        return hash(self.property)

    def __eq__(self, other):
        # print "Eq called for: "
        # print self
        # print other
        return (self.property == other.property)

    def __ne__ (self, other):
        return not (self == other)

    def __str__(self):
        return (self.name + "$" + self.property)


class Property(Enum):
    cool = Dummy("foo", "cool")
    hot = Dummy("bar", "hot")

虽然这工作正常,但我注意到——通过取消注释 print 语句——为两个枚举成员值调用了 __hash____eq__ 魔法方法.为什么会这样?这些不是仅在散列和相等性检查期间使用吗?

此外,如果我将枚举 class 更改为以下内容,一切都会变得一团糟。

class Property(Enum):
    cool = Dummy("foo", "cool")
    hot = [Dummy("bar-1", "hot-1"), Dummy("bar-2", "hot-2")]

__eq__ 魔术方法似乎是为 Property.cool 对应的 Dummy 对象和 Property.hot 对应的列表调用的,从输出可以看出:

Hash called for  foo$cool
----
Eq called for: 
foo$cool
[<__main__.Dummy object at 0x7fd36633f2d0>, <__main__.Dummy object at 0x7fd36633f310>]
----
Traceback (most recent call last):
  File "test.py", line 28, in <module>
    class Property(Enum):
  File "/blah/.local/lib/python2.7/site-packages/enum/__init__.py", line 237, in __new__
    if canonical_member.value == enum_member._value_:
  File "test.py", line 19, in __eq__
    return (self.property is other.property)
AttributeError: 'list' object has no attribute 'property'

为什么会这样?为什么首先调用魔术方法,为什么 __eq__ 在 class 对象和列表上调用?

请注意,这只是一个代表性示例,真正的用例使这种设计——将值作为可哈希 class 对象列表的枚举——看起来不那么奇怪。

Enum class 正在比较它的成员对象值以查看是否有任何是另一个的别名。例如,在下面的枚举中,ab 都表示相同的值,因此只有 a 应该出现在成员列表中(别名不会):

class A(Enum):
    a=1
    b=1

您可以通过查看进行相等性检查的行的源代码来验证这一点:source

对于散列,这样做是为了提供枚举成员的按值查找。同样,这可以在 source code

中找到