The comparison operator methods

Python has six comparison operators. These operators have special method implementations. According to the documentation, mapping works as follows:

  • x<y is implemented by x.__lt__(y).
  • x<=y is implemented by x.__le__(y).
  • x==y is implemented by x.__eq__(y).
  • x!=y is implemented by x.__ne__(y).
  • x>y is implemented by x.__gt__(y).
  • x>=y is implemented by x.__ge__(y).

We'll return to comparison operators again when looking at numbers in Chapter 8, Creating Numbers.

There's an additional rule regarding what operators are actually implemented that's relevant here. These rules are based on the idea that the object's class on the left-hand side of an operator defines the required special method. If it doesn't, Python ...

Get Mastering Object-Oriented Python - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.