我创建了一个名为point的类,如下所示:
class point:
def __init__(self):
self.x = 0
self.y = 0
并创建一个点实例列表:
p1 = point()
p1.x = 1
p1.y = 1
p2 = point()
p2.x = 2
p2.y = 2
p_list = []
p_list.append(p1)
p_list.append(p2)
现在我想从列表中删除x = 1和y = 1的实例,我该怎么做?
我尝试为类点添加__cmp__方法如下:
class point:
def __init__(self):
self.x = 0
self.y = 0
def __cmp__(self, p):
return self.x==p.x and self.y==p.y
但是以下代码不起作用
r = point()
r.x = 1
r.y = 1
if r in p_list:
print('correct')
else:
print('wrong') # it will go here
p_list.remove(r) # it reports 'ValueError: list.remove(x): x not in list'