Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Comparison Magic Methods | Magic Methods
In-Depth Python OOP

Comparison Magic Methods

Similarly to the mathematical magic methods, here we take self and other as arguments and compare their desired attributes. These magic methods should always return True or False.

Magic MethodOperation
__eq__(self, other)==
__gt__(self, other)>
__ge__(self, other)>=
__lt__(self, other)<
__le__(self, other)<=
__ne__(self, other)!=

Note

To understand the naming of these methods, look at some examples:

  • eq is equal.
  • gt is greater than.
  • le is less or equal.
  • ne is not equal.

Look at the example of Road class:

Code Description
The Road class has implemented the __eq__ method, which is the equality operator (==) in Python. This method allows objects of the Road class to be compared for equality with other objects.

The __eq__ method first checks if the other object is an instance of the Road class using the isinstance() function. If it is, it compares the length attribute of the current Road object (self.length) with the length attribute of the other Road object (other.length). If the other object is not an instance of the Road class, it assumes that other is a numeric value and compares the length attribute of the current Road object (self.length) with the other value directly.

In summary, the __eq__ method of the Road class allows two Road objects to be compared for equality based on their length attributes. It also allows a Road object to be compared with a numeric value for equality, considering only the length attribute of the Road object.

Note

To optimize the code, you can utilize existing comparison methods. For example, if you have implemented the __lt__() method (<), by implementing the __ge__() method (>=), you can use return not (instance1 < instance2) which will return the opposite result of the already implemented < operator.

Which magic method should be used?

Select the correct answer

Everything was clear?

Section 5. Chapter 4
course content

Course Content

In-Depth Python OOP

Comparison Magic Methods

Similarly to the mathematical magic methods, here we take self and other as arguments and compare their desired attributes. These magic methods should always return True or False.

Magic MethodOperation
__eq__(self, other)==
__gt__(self, other)>
__ge__(self, other)>=
__lt__(self, other)<
__le__(self, other)<=
__ne__(self, other)!=

Note

To understand the naming of these methods, look at some examples:

  • eq is equal.
  • gt is greater than.
  • le is less or equal.
  • ne is not equal.

Look at the example of Road class:

Code Description
The Road class has implemented the __eq__ method, which is the equality operator (==) in Python. This method allows objects of the Road class to be compared for equality with other objects.

The __eq__ method first checks if the other object is an instance of the Road class using the isinstance() function. If it is, it compares the length attribute of the current Road object (self.length) with the length attribute of the other Road object (other.length). If the other object is not an instance of the Road class, it assumes that other is a numeric value and compares the length attribute of the current Road object (self.length) with the other value directly.

In summary, the __eq__ method of the Road class allows two Road objects to be compared for equality based on their length attributes. It also allows a Road object to be compared with a numeric value for equality, considering only the length attribute of the Road object.

Note

To optimize the code, you can utilize existing comparison methods. For example, if you have implemented the __lt__() method (<), by implementing the __ge__() method (>=), you can use return not (instance1 < instance2) which will return the opposite result of the already implemented < operator.

Which magic method should be used?

Select the correct answer

Everything was clear?

Section 5. Chapter 4
some-alt