How to compare two instances of an object Python?
How to compare two instances of an object Python?
Attempting to compare two objects data members; however, the error message has no specific details, which leaves me with little information on how to go about correcting it
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))
4 Answers
4
The other answers are correct and provide the best way to do it, but I realized that you wrote:
print calls provided as part of an exercise: not my implementation
print(same_person(person1, person2))
print(same_person(person1, person3))
The exercise probably wants you to define a function outside the class. You can do that by removing that function from the class and writing it un-indented outside the class (without providing class type too). For example:
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))
Much appreciated
– aguilar
Sep 1 at 20:06
Glad to help :-)
– Moe A
Sep 1 at 20:06
@aguilar keep in mind that the best way to compare two objects would be overriding
__eq__()
method of the class, have a look at this– Moe A
Sep 1 at 20:10
__eq__()
same_person is a method of the class Person
and should take just an argument as input. It should be defined as:
Person
def same_person(self, other):
return self.id == other.id
and called as
person1.same_person(person2)
or you could override the __eq__
method (i.e., ==
).
__eq__
==
def __eq__(self, other):
return self.id == other.id
in order to be able to do it as person1 == person2
person1 == person2
return self is other
is a bit more elegant.– DYZ
Sep 1 at 19:45
return self is other
@DYZ: but then
Person(1, 2, 3) != Person(1, 2, 3)
, since they're not the same object.– Blender
Sep 1 at 19:54
Person(1, 2, 3) != Person(1, 2, 3)
If you suggest to add
self.id is other.id
I don't really understand why, we are checking equality not object identity.– abc
Sep 1 at 19:57
self.id is other.id
I suggest to replace
self.id == other.id
with self is other
(both expressions are fully equivalent).– DYZ
Sep 1 at 20:13
self.id == other.id
self is other
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
you dont have to define lhs and rhs type in python unless you are using typings.
Quite a few mistakes:
Person
person1
person2
person3
same_person
This is what I would do:
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))
Output:
True
False
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Among other things, you are probably mixing tabs and spaces in your indentation somewhere.
– DYZ
Sep 1 at 19:40