Summary: In this tutorial, you will learn what is __lt__ in Python and how can you use it in your Python program.

__lt__ in Python

In Python, __lt__ is a special method that is used to implement the less than operator <.

It is a part of the set of special methods known as “magic methods” or “dunder methods” (short for “double underscore”).

When the < operator is used on an object, Python checks to see if the __lt__ method is defined for that object’s class, and if so, it calls that method to determine the result of the comparison.

If the __lt__ method is not defined, Python will fall back to using the built-in comparison method for the object’s type.

Consider the following example for instance:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __lt__(self, other):
        return self.age < other.age

p1 = Person("Alice", 25)
p2 = Person("Bob", 30)

print(p1 < p2) #True

In this example, the Person class has an __init__ method that sets the name and age attributes, and an __lt__ method that compares the age of two Person objects.

The < operator is used to compare two instances of the Person class and the __lt__ method is called to determine the result. Since the age of p1 is less than the age of p2, the output is True.

If the __lt__ method is not implemented for the Person class in the example I provided, the < operator will not know how to compare two Person objects and will raise a TypeError:

TypeError: '<' not supported between instances of 'Person' and 'Person'

This is because the default comparison method for a user-defined class is the id() function, which returns a unique identifier for an object and is not useful for comparing the attributes of two Person objects.

Note: The __lt__ method should return True when the object on the left side is considered “less than” the object on the right side, and False otherwise.

When to use __lt__?

You should use __lt__ method when you want to define a custom comparison for instances of a class using the less than operator <.

This method is called when the < operator is used to compare two instances of a class and it should return True if the object on the left side is considered “less than” the object on the right side, and False otherwise.

For example, if you wanted to create a custom class for a deck of cards and define a comparison based on the card rank, you could use the __lt__ method to compare the rank of two cards and determine which one is “greater” or “lesser”.

Custom Sorting Order for Objects

You can also use __lt__ if you want to define a custom sorting order for instances of your class, as the < operator is used by the built-in sorted() function and other sorting algorithms.

For Example:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __lt__(self, other):
        return self.age < other.age

people = [Person("Alice", 25), Person("Bob", 30), Person("Charlie", 20)]

sorted_people = sorted(people)

for person in sorted_people:
    print(f"{person.name} is {person.age} years old.")

Output:

Charlie is 20 years old.
Alice is 25 years old.
Bob is 30 years old.

In this example, the Person class has an __init__ method that sets the name and age attributes, and an __lt__ method that compares the age of two Person objects.

The sorted() function is used to sort the list of Person objects, and it uses the __lt__ method to determine the order of the objects.

Since the __lt__ method compares the age of two Person objects, the sorted() function will sort the list of Person objects by age.

It is also possible to write the above program without the __lt__ method. The sorted() function also accepts a key function that is used to compare the objects. You can use key=lambda x: x.age instead of __lt__ method and get the same result.

Adarsh Kumar

I am an engineer by education and writer by passion. I started this blog to share my little programming wisdom with other programmers out there. Hope it helps you.

Leave a Reply