Unexpected behavior for python set.__contains__(python set.__contains__ 的意外行为)
问题描述
从 __contains__
文档中借用文档
print set.__contains__.__doc__
x.__contains__(y) <==> y in x.
这似乎适用于 int、basestring 等原始对象.但对于定义 __ne__
和 __eq__
方法的用户定义对象,我感到意外行为.这是一个示例代码:
This seems to work fine for primitive objects such as int, basestring, etc. But for user-defined objects that define the __ne__
and __eq__
methods, I get unexpected behavior. Here is a sample code:
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
那么这是一个错误还是一个功能?
So is this a bug or a feature?
推荐答案
对于set
s和dicts
,需要定义__hash__
.任何两个相等的对象都应该散列相同,以便在 set
s 和 dicts
中获得一致/预期的行为.
For set
s and dicts
, you need to define __hash__
. Any two objects that are equal should hash the same in order to get consistent / expected behavior in set
s and dicts
.
我建议使用 _key
方法,然后在需要比较项目部分的任何地方引用它,就像从 调用
而不是重新实现它:__eq__
__ne__
I would reccomend using a _key
method, and then just referencing that anywhere you need the part of the item to compare, just as you call __eq__
from __ne__
instead of reimplementing it:
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)
这篇关于python set.__contains__ 的意外行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:python set.__contains__ 的意外行为


基础教程推荐
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01