如何比较Python对象的两个实例?
试图比较两个对象的数据成员;然而,错误消息没有具体的细节,这使我几乎没有关于如何纠正它的信息如何比较Python对象的两个实例?,python,equality,Python,Equality,试图比较两个对象的数据成员;然而,错误消息没有具体的细节,这使我几乎没有关于如何纠正它的信息 class Person: def __init__(self, name, age, id): self.name = name self.age = age self.id = id def same_person(Person lhs, Person rhs): return lhs.id == rhs.id person1 = Person("Dav
class Person:
def __init__(self, name, age, id):
self.name = name
self.age = age
self.id = id
def same_person(Person lhs, Person rhs):
return lhs.id == rhs.id
person1 = Person("David Joyner", 30, 901234567)
person2 = Person("D. Joyner", 29, 901234567)
person3 = Person("David Joyner", 30, 903987654)
# print calls provided as part of an exercise: not my implementation
print(same_person(person1, person2))
print(same_person(person1, person3))
- Python 3.6.5
- 命令:python person.py
- 错误消息
- 如果是缩进级别,则显示以下错误
- 同一个人是类
person
的一种方法,应该只接受一个参数作为输入。应将其定义为:
def same_person(self, other):
return self.id == other.id
并称为
person1.same_person(person2)
或者您可以覆盖\uuu eq\uu
方法(即=
)
为了能够像
person1==person2
那样做,同一个人是类person
的一个方法,应该只接受一个参数作为输入。应将其定义为:
def same_person(self, other):
return self.id == other.id
class Person:
def __init__(self, name, age, id):
self.name = name
self.age = age
self.id = id
def same_person(self, lhs, rhs):
return lhs.id == rhs.id
并称为
person1.same_person(person2)
或者您可以覆盖\uuu eq\uu
方法(即=
)
为了能够像person1==person2
class Person:
def __init__(self, name, age, id):
self.name = name
self.age = age
self.id = id
def same_person(self, lhs, rhs):
return lhs.id == rhs.id
除非使用打字,否则不必在python中定义lhs和rhs类型
除非使用打字,否则不必在python中定义lhs和rhs类型。有很多错误:
Person
classnameperson1
、person2
和person3
same\u person
),则应在实例上使用该方法class Person:
def __init__(self, name, age, id):
self.name = name
self.age = age
self.id = id
def same_person(self, other):
return self.id == other.id
person1 = Person("Bob", 25, 1)
person2 = Person("Mike", 33, 1)
person3 = Person("Maria", 28, 2)
print(person1.same_person(person2))
print(person1.same_person(person3))
输出:
True
False
不少错误:
Person
classnameperson1
、person2
和person3
same\u person
),则应在实例上使用该方法class Person:
def __init__(self, name, age, id):
self.name = name
self.age = age
self.id = id
def same_person(self, other):
return self.id == other.id
person1 = Person("Bob", 25, 1)
person2 = Person("Mike", 33, 1)
person3 = Person("Maria", 28, 2)
print(person1.same_person(person2))
print(person1.same_person(person3))
输出:
True
False
其他答案是正确的,并提供了最好的方法,但我意识到你写道: 打印作为练习的一部分提供的调用:不是我的实现 本练习可能希望您在类之外定义一个函数。您可以通过从类中删除该函数并在类外不缩进地编写它(不提供类类型)来实现这一点。例如:
class Person:
def __init__(self, name, age, id):
self.name = name
self.age = age
self.id = id
def same_person(lhs, rhs):
return lhs.id == rhs.id
person1 = Person("David Joyner", 30, 901234567)
person2 = Person("D. Joyner", 29, 901234567)
person3 = Person("David Joyner", 30, 903987654)
print(same_person(person1, person2))
print(same_person(person1, person3))
其他答案是正确的,并提供了最好的方法,但我意识到你写道: 打印作为练习的一部分提供的调用:不是我的实现 本练习可能希望您在类之外定义一个函数。您可以通过从类中删除该函数并在类外不缩进地编写它(不提供类类型)来实现这一点。例如:
class Person:
def __init__(self, name, age, id):
self.name = name
self.age = age
self.id = id
def same_person(lhs, rhs):
return lhs.id == rhs.id
person1 = Person("David Joyner", 30, 901234567)
person2 = Person("D. Joyner", 29, 901234567)
person3 = Person("David Joyner", 30, 903987654)
print(same_person(person1, person2))
print(same_person(person1, person3))
您最好重写eq以比较对象:
class Person:
def __init__(self, name, age, id):
self.name = name
self.age = age
self.id = id
def __eq__(self, other):
return self.id == other.id
您最好重写eq以比较对象:
class Person:
def __init__(self, name, age, id):
self.name = name
self.age = age
self.id = id
def __eq__(self, other):
return self.id == other.id
除此之外,您可能在缩进中的某个位置混合了制表符和空格。除此之外,您可能在缩进中的某个位置混合了制表符和空格。
返回自我是另一个
更优雅。@DYZ:但是人(1,2,3)!=Person(1,2,3)
,因为它们不是同一个对象。如果您建议添加self.id is other.id
,我真的不明白为什么,我们检查的是相等而不是对象标识。我建议将self.id==other.id
替换为self is other
(两个表达式完全等效).return self is other
更优雅一点。@DYZ:但是Person(1,2,3)!=Person(1,2,3)
,因为它们不是同一个对象。如果您建议添加self.id is other.id
,我真的不明白为什么,我们检查的是相等而不是对象标识。我建议将self.id==other.id
替换为self is other
(两个表达式完全等效)。非常感谢乐于帮助:-)@aguilar请记住,比较两个对象的最佳方法是重写类的\uuuu eq\uuu()
方法,请看一看非常感谢乐于帮助:-)@aguilar请记住,比较两个对象的最佳方法是重写类的\uu eq\uuuu()
方法,请看一看