andres
andres

Reputation: 321

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

Upvotes: 3

Views: 7751

Answers (5)

tomgtbst
tomgtbst

Reputation: 77

You'd better rewrite eq to compare objects:

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
person1 = Person("Bob", 25, 1)
person2 = Person("Mike", 33, 1)
person3 = Person("Maria", 28, 2)

print(person1 == person2)
print(person1 == person3)
>>> True
>>> False

https://devinpractice.com/2016/11/29/python-objects-comparison/

Upvotes: 0

Mohd
Mohd

Reputation: 5613

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

Upvotes: 1

Quite a few mistakes:

  1. The arguments in the method cannot be preceded by the Person classname
  2. You have not defined instances person1, person2 and person3
  3. If you define an instance method (same_person), it should be used ON an instance.

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

Upvotes: 1

abc
abc

Reputation: 11929

same_person is a method of the class Person and should take just an argument as input. It should be defined as:

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., ==).

def __eq__(self, other):
    return self.id == other.id

in order to be able to do it as person1 == person2

Upvotes: 3

GraphicalDot
GraphicalDot

Reputation: 2821

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.

Upvotes: 1

Related Questions