You can use the total_ordering decorator from functools, which generates all the missing compare methods if you supply __eq__() and one other.

Given a class defining one or more rich comparison ordering methods, this class decorator supplies the rest. This simplifies the effort involved in specifying all of the possible rich comparison operations:

The class must define one of __lt__(), __le__(), __gt__(), or __ge__(). In addition, the class should supply an __eq__() method.

For Example,

import functools


@functools.total_ordering
class Student:
    def _is_valid_operand(self, other):
        return (hasattr(other, "lastname") and
                hasattr(other, "firstname"))

    def __eq__(self, other):
        if not self._is_valid_operand(other):
            return NotImplemented
        return ((self.lastname.lower(), self.firstname.lower()) ==
                (other.lastname.lower(), other.firstname.lower()))

    def __lt__(self, other):
        if not self._is_valid_operand(other):
            return NotImplemented
        return ((self.lastname.lower(), self.firstname.lower()) <
                (other.lastname.lower(), other.firstname.lower()))
Answer from Juri Robl on Stack Overflow
🌐
O'Reilly
oreilly.com › library › view › making-classes-support › 9781491965276 › ch01.html
Making Classes Support Comparison Operations - Making classes support comparison operations in Python [Book]
August 15, 2016 - As an example, let’s build some houses and add some rooms to them, and then perform comparisons based on the size of the houses: from functools import total_ordering class Room(object): def __init__(self, name, length, width): self.name = name self.length = length self.width = width self.square_feet = self.length * self.width @total_ordering class House(object): def __init__(self, name, style): self.name = name self.style = style self.rooms = list() @property def living_space_footage(self): return sum(r.square_feet for r in self.rooms) def add_room(self, room): self.rooms.append(room) def __str__(self): return '{}: {} square foot {}'.format(self ...
Authors   Brian K. JonesDavid Beazley
Published   2016
Pages   20
Top answer
1 of 4
52

You can use the total_ordering decorator from functools, which generates all the missing compare methods if you supply __eq__() and one other.

Given a class defining one or more rich comparison ordering methods, this class decorator supplies the rest. This simplifies the effort involved in specifying all of the possible rich comparison operations:

The class must define one of __lt__(), __le__(), __gt__(), or __ge__(). In addition, the class should supply an __eq__() method.

For Example,

import functools


@functools.total_ordering
class Student:
    def _is_valid_operand(self, other):
        return (hasattr(other, "lastname") and
                hasattr(other, "firstname"))

    def __eq__(self, other):
        if not self._is_valid_operand(other):
            return NotImplemented
        return ((self.lastname.lower(), self.firstname.lower()) ==
                (other.lastname.lower(), other.firstname.lower()))

    def __lt__(self, other):
        if not self._is_valid_operand(other):
            return NotImplemented
        return ((self.lastname.lower(), self.firstname.lower()) <
                (other.lastname.lower(), other.firstname.lower()))
2 of 4
26

Define or override the comparison operators for the class. http://docs.python.org/reference/expressions.html#notin

Looks like you are on the right track, except you only need to pass the second circle object to your comparison. self refers to the first circle object. So self.r would give you the r of the first circle. Also you need to return True or False from the method.

def __gt__(self, circle2):
    return self.r > circle2.r

Note that this is just comparing the r's of the circles.

🌐
W3Schools
w3schools.com › python › gloss_python_comparison_operators.asp
Python Comparison Operators
Python If Python Elif Python Else Shorthand If Logical Operators Nested If Pass Statement Code Challenge Python Match
🌐
TutorialsPoint
tutorialspoint.com › How-to-overload-Python-comparison-operators
How to overload Python comparison operators?
Python has magic methods to define overloaded behaviour of operators. The comparison operators (<, <=, >, >=, == and !=) can be overloaded by providing definition to __lt__, __le__, __gt__, __ge__, __eq__ and __ne__ magic methods. Following program overloads == and >= operators to compare objects ...
🌐
StudySmarter
studysmarter.co.uk › python comparison operators
Python Comparison Operators: Definition & Examples
Special methods in Python custom ... Python provides several comparison operators: `==` (equal to), `!=` (not equal to), `<` (less than), `<=` (less than or equal to), `>` (greater than), and `>=` (greater than or equal to)....
🌐
Codearmo
codearmo.com › python-tutorial › object-orientated-programming-comparison-methods
Comparison Methods | Codearmo
March 28, 2025 - - To compare two objects we need to implement the built in __comparison__ operators. - If we implement the < and <= methods or the > or >= or equal methods we get the others for free, since Python just flips the symbols if it can't find the ...
🌐
Readthedocs
portingguide.readthedocs.io › en › latest › comparisons.html
Comparing and Sorting — Conservative Python 3 Porting Guide 1.0 documentation
To avoid the hassle of providing all six functions, you can implement __eq__, __ne__, and only one of the ordering operators, and use the functools.total_ordering() decorator to fill in the rest. Note that the decorator is not available in Python 2.6. If you need to support that version, you’ll need to supply all six methods. The @total_ordering decorator does come with the cost of somewhat slower execution and more complex stack traces for the derived comparison methods, so defining all six explicitly may be necessary in some cases even if Python 2.6 support is dropped.
🌐
Real Python
realpython.com › python-is-identity-vs-equality
Python != Is Not is not: Comparing Objects in Python – Real Python
September 7, 2023 - ... As a rule of thumb, you should always use the equality operators == and !=, except when you’re comparing to None: Use the Python == and != operators to compare object equality.
🌐
Mostly Python
mostlypython.com › oop-in-python-part-8-comparing-objects
OOP in Python, part 8: Comparing objects - by Eric Matthes
March 27, 2024 - class River: def __init__(self, name, length=0): ... def __gt__(self, river_2): return self.length > river_2.length · Here we’re defining how to make a comparison between two rivers using the > operator. Python should compare the length of each river, and return the resulting Boolean value.
Find elsewhere
🌐
Python
docs.python.org › 3 › library › operator.html
operator — Standard operators as functions
Perform “rich comparisons” between a and b. Specifically, lt(a, b) is equivalent to a < b, le(a, b) is equivalent to a <= b, eq(a, b) is equivalent to a == b, ne(a, b) is equivalent to a != b, gt(a, b) is equivalent to a > b and ge(a, b) ...
🌐
Glennrowe
glennrowe.net › programmingpages › 2021 › 06 › 17 › overloading-comparison-operators
Overloading comparison operators – Programming pages
For arithmetic operators, we needed a special overload to handle this case, but for comparison operators, Python automatically switches the operands to do the comparison. Thus print(20 == rec3) is converted to print(rec3 == 20). The other four comparison operators can be overloaded in the same way. Here’s a complete Rectangle class with all 6 operators overloaded:
🌐
Wiingy
wiingy.com › home › learn › python › operator overloading in python
Operator Overloading in Python
January 30, 2025 - Here’s an example of how operator ... we can also overload other operators in Python, such as the comparison operators (<, >, <=, >=, ==, and !=)....
🌐
Python Tutorial
pythontutorial.net › home › python oop › python __eq__
Python __eq__
March 31, 2025 - Python automatically calls the __eq__ method of a class when you use the == operator to compare the instances of the class.
🌐
Exercism
exercism.org › tracks › python › concepts › comparisons
Comparisons in Python on Exercism
The table below shows the most common Python comparison operators: They all have the same priority (which is higher than that of Boolean operations, but lower than that of arithmetic or bitwise operations). Objects that are different types (except numeric types) never compare equal by default. Non-identical instances of a class will also not compare as equal unless the class defines special rich comparison methods that customize the default object comparison behavior.
🌐
Pluralsight
pluralsight.com › labs › aws › overriding-comparison-operators-in-python
Overriding Comparison Operators in Python
The first set of operators that you need to implement are the == and != operators. These should compare the lead score of the two Lead instances being compared. You can implement these using the __eq__ and __ne__ methods, and these methods should ...
🌐
Educative
educative.io › answers › what-are-comparison-operators-in-python
What are comparison operators in Python?
Comparison or relational operators in Python are used to compare variables with values.
🌐
DataFlair
data-flair.training › blogs › python-comparison-operators
Python Comparison Operators with Syntax and Examples - DataFlair
July 12, 2025 - Python Comparison Operators -Learn Python less than,Python greater than,equal to,not equal to less than,greater than or equal to Operators syntax & Example
🌐
TutorialsPoint
tutorialspoint.com › how-do-we-use-equivalence-equality-operator-in-python-classes
How do we use equivalence (“equality”) operator in Python classes?
Even when both instances have the same area for the square attribute, the result of comparing the objects using the == operator will be False. This is shown in the following code ? class Square: def __init__(self, area): self.square = area sq1 = Square(23) sq2 = Square(23) result = sq1 == sq2 ...
🌐
Algor Education
cards.algoreducation.com › en › content › zI6ImimS › python-comparison-operators
Python Comparison Operators | Algor Cards
Python extends the functionality ... These include __eq__ (equal to), __ne__ (not equal to), __gt__ (greater than), __lt__ (less than), __ge__ (greater than or equal to), and __le__ (less than or equal to)....