Here we are going to learn about one of the magical operators in python. These magic operators are the special operators. Now we will learn about python __lt__ magical operator. These magical operators are useful to define the behavior of operators. __lt__ is a special method for less than operator(<). Usually, we are using less than operator to check the less than numbers. Already we learned about the less-than operator in the topic of The Secret of Comparators | Comparision Operators In Python.
Magical methods are something useful to define operators in python. There are a lot of magical operators available in python. Python __lt__ is a method that is useful to define the less-than operator. __gt__ is useful to define greater than. Likewise, we have a lot of magical operators. We are using double underscore to represent the magical methods. These magical methods cannot be called directly. These special methods are useful in operator overloading.
What is Python __lt__ magical operator?
__lt__ is a special method that describes the less-than operator in python. > is a symbol for the less-than operator. It is denoted with a double underscore to represent the special method. This method is not called directly like the less-than operator.
Python __lt__ magical operator uses
- It describes the less than operator.
- __lt__ is useful for sorting
- It is useful for operator overloading
- __lt__ is useful for comparing
Definition of Python __lt__ magical operator
As we already know __lt__ operator cannot be used directly. We can define it as follows:
__lt__(self, other)
Magical Operator
Magical Comparison Operators | Description |
---|---|
__lt__ | describes less than operator(<) |
__le__ | descries less than or equal to (<=) |
__gt__ | describes greater than (>) |
__ge__ | describes greater than or equal to (>=) |
__eq__ | describes equality operator(==) |
__ne__ | describes not equal to operator(!=) |
Using Python __lt__ to compare the weight of two persons
class weight:
def __init__(self, weight):
self. weight= weight
def __lt__(self,other):
return self.weight<other.weight
a=weight(50)
b=weight(60)
print(a<b)
Creating a class named “weight”. Defining a __init__ function. Inside a function declaring the weight. Creating another function named __lt__. This function is to compare the weights. Variables a and b holds the weight of two different persons. It compares the weight if the condition is true. It will display the result as True. Otherwise False.
Output
True
Using Python __lt__ to compare the weight of multiple persons
class weight:
def __init__(self, weight):
self. weight= weight
def __lt__(self,other):
return self.weight<other.weight
def __gt__(self,other):
return self.weight>other.weight
a=weight(50)
b=weight(60)
c=weight(70)
print(a<b and b>c)
Creating a class named “weight”. Defining a __init__ function. Inside a function declaring the weight. Creating another function named __lt__ and followed by __gt__. These functions are to compare the weights. Variables a, b, and c holds the weight of three different persons. It compares the weight if the condition is true; it will display the result as True. Otherwise False.
Output
False
Using __lt__ for operator overloading
class overload:
a=0
def __init__(self,operator):
self.a=operator
def __lt__(self,other):
print("less than operator overloaded")
if self.a<other.a:
return True
else:
return False
x=overload(15)
y=overload(9)
print(f'x<y = {x < y}')
Creating a class overload. Defining a function init. Creating another function named lt. Using an if-else statement inside a function. If the number on the left-hand side is lesser than the right-hand side, it will display True. Otherwise False.
Output
less than operator overloaded x<y = False
Using Python __lt__ to sort
import math
from functools import total_ordering
class point:
def __init__(self,a,b):
self.a = a
self.b = b
def __repr__(self):
return f'Point(a={self.a}, b={self.b})'
@property
def compare(self):
return math.sqrt(self.a * self.a + self.b * self.b)
def __lt__(self, other):
return self.compare< other.compare
print(sorted([
point(10, 5), point(30, 10), point(17, 7), point(2, 0)
]))
Importing math module. Creating a class named point. It will sort all the points. Here we are importing functools that is to support the less-than operator. Using a formatted function to display the output.
Output
[Point(a=2, b=0), Point(a=10, b=5), Point(a=17, b=7), Point(a=30, b=10)]
FAQs Related to Python __lt__ Magical Operator
No, we can’t call the __lt__ magical operator directly.
By using we can use it to sort the list, operator overloading, and for comparing.
Final Thoughts
In this article, we have learned about __lt__ in a detailed manner. We have learned What is __lt__ magical operator? How to define it? What are the uses of it? We hope this article is beneficial. Try to implement the program on your own. Learn with us!
great! thank you