john = Person('John', 'Doe', 25)
jane = Person('Jane', 'Doe', 25)
Code language:Python(python)
In this example, the john and jane objects are not the same object. And you can check it
using the is operator:
print(john is jane) # False
Code language:Python(python)
Also, when you compare john with jane using the equal operator (==), you’ll get the result of False:
print(john == jane) # False
Code language:Python(python)
Since john and jane have the same age, you want them to be equal. In other words, you want the following expression to return True:
john == jane
To do it, you can implement the __eq__ dunder method in the Person class.
Python automatically calls the __eq__ method of a class when you use the == operator to
compare the instances of the class. By default, Python uses the is operator if you don’t provide a specific implementation for the __eq__ method.
The following shows how to implement the __eq__ method in the Person class that returns True if two person objects have the same age: