python - 如何为需要比较同一类的自定义对象定义 __eq__ func
问题描述
我想__eq__()
为自定义类实现一个方法Vertex
。
但是,当我仔细考虑这个问题时,我发现它很奇怪。
例如
class Vertex():
def __init__(self,node):
self.id = node
self.adjacent = {}
但是对于相邻的dict
,它存储这样的数据:
{neighbour_vertex1 : edge, neighbour_vertex2 : edge, ....}
如果我想实现该__eq__()
方法,它应该如下所示:
def __eq__(self,other):
return self.id == other and self.adjacent == other.adjacent
但self.adjacent == other.adjacent
需要比较听写
{neighbour_vertex1 : edge, neighbour_vertex2 : edge, ....}
{neighbour_vertex1 : edge, neighbour_vertex2 : edge, ....}
为了比较它们,我必须定义neighbout_vertex
确实是 class 实例的比较函数Vertex
。
我认为这就像一个先有鸡还是先有蛋的问题,任何建议都值得赞赏。
编辑:示例
class Vertex(object):
def __init__(self,id):
self.id = id
self.adjacent ={}
def add_vertex(self,obj):
self.adjacent.update(obj)
def __eq__(self,other):
return self.id == other.id and self.adjacent == other.adjacent
def __hash__(self):
return hash(id(self))
obj1 = Vertex("one")
obj2 = Vertex("two")
temp = {obj2:"connect 1-2"}
obj1.add_vertex(temp)
obj1s = Vertex("one")
obj2s = Vertex("two")
temp2 = {obj2s:"connect 1-2"}
obj1s.add_vertex(temp2)
if obj1 == obj1s:
print("True")
else:
print("False")
我拿出了一个简单的例子,首先我不想修改hash函数,如何修改__eq__函数让上面的代码输出
True
而不是False
解决方案
我认为您根本不需要在__eq__
这里覆盖。Vertex
使用您选择的设计,两个不同的实例表示“相同的顶点”是没有意义的,并且两个Vertex
实例只有在它们实际上是同一个对象时才应该比较相等。默认__eq__
实现已经为您提供了该行为。
如果您确实希望不同的实例Vertex
比较相等,那么您需要首先考虑您的设计。例如,您对__hash__
as的定义hash(id(self))
仅在平等通过身份起作用时才有意义,因此您__hash__
需要更改。你还需要考虑一些事情,如果顶点a
和b
都有self.id = 1
和self.adjacent = {c: 'asdf'}
,但是c
有self.adjacent = {b: 'asdf'}
怎么办?是a
和b
平等的吗?你将不得不对你想要使用的平等概念提出一个精确的定义。
一种可能的重新设计是将邻接跟踪移出顶点对象。如果顶点对象只有self.id
,并且边缘被某种 Graph 对象从外部跟踪,那么“按值”比较 Vertex 对象可能是有意义的。不过,到那时,完全消除 Vertex 类可能更有意义。
推荐阅读
- r - 如何通过在R中一次只取两个值的函数循环向量?
- sql - 根据每个 ID 的另一列的值查找一个列的两个值
- numpy - Numpy 改进或避免循环
- c++ - 找到最小的正数
- javascript - 更新 JSON 文件写入在 NodeJS 中不起作用
- javascript - 如何让 A 和 B 并行运行?
- react-native - 我们可以传递隐藏在 React Native 中的 id 吗?
- node.js - node.js 应用程序中的 Firestore 快照侦听器限制
- python - 如何在 Python 中为用户输入着色?
- python - 如何阅读保存在本地驱动器上的 pdf 文档?找不到文件或目录错误