【问题标题】:Unexpected behavior for python set.__contains__python set.__contains__ 的意外行为
【发布时间】:2011-11-24 20:52:04
【问题描述】:

__contains__ 文档中借用文档

print set.__contains__.__doc__
x.__contains__(y) <==> y in x.

这似乎适用于 int、basestring 等原始对象。但对于定义 __ne____eq__ 方法的用户定义对象,我会遇到意外行为。这是一个示例代码:

class CA(object):
  def __init__(self,name):
    self.name = name

  def __eq__(self,other):
    if self.name == other.name:
      return True
    return False

  def __ne__(self,other):
    return not self.__eq__(other)

obj1 = CA('hello')
obj2 = CA('hello')

theList = [obj1,]
theSet = set(theList)

# Test 1: list
print (obj2 in theList)  # return True

# Test 2: set weird
print (obj2 in theSet)  # return False  unexpected

# Test 3: iterating over the set
found = False
for x in theSet:
  if x == obj2:
    found = True

print found   # return True

# Test 4: Typcasting the set to a list
print (obj2 in list(theSet))  # return True

那么这是一个错误还是一个功能?

【问题讨论】:

  • 这个问题应该在 stackoverflow 上显示:如何提问。清楚的点,用一个小例子说明问题。正如其他人在这里回答的那样,集合使用哈希值,否则他们将获得列表的性能..
  • 样式说明:使用return self.name == other.name而不是if cond: return True \n return False

标签: python list set


【解决方案1】:

对于sets 和dicts,您需要定义__hash__。任何两个相等的对象都应该散列相同,以便在sets 和dicts 中获得一致/预期的行为。

我建议使用_key 方法,然后在任何需要比较项目的部分的地方引用它,就像从__ne__ 调用__eq__ 而不是重新实现它:

class CA(object):
  def __init__(self,name):
    self.name = name

  def _key(self):
    return type(self), self.name

  def __hash__(self):
    return hash(self._key())

  def __eq__(self,other):
    if self._key() == other._key():
      return True
    return False

  def __ne__(self,other):
    return not self.__eq__(other)

【讨论】:

    【解决方案2】:

    这是因为CA 没有实现__hash__

    一个合理的实现是:

    def __hash__(self):
        return hash(self.name)
    

    【讨论】:

      【解决方案3】:

      A set hashes 它是允许快速查找的元素。您必须覆盖 __hash__ 方法才能找到元素:

      class CA(object):
        def __hash__(self):
          return hash(self.name)
      

      列表不使用散列,而是像 for 循环那样比较每个元素。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-22
        • 1970-01-01
        • 2017-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多