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))
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))