Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Python __lt__

"""
Magic method __lt__ stands for less than. So this magic method defines or 
overwrites what should "<" sign do with objects.
"""

class Test:
    def __init__(self, x):
        self.x = x
    
    def __lt__(self, other):
        """
		Compares attribute "x" of objects, self stands for 1st object
		and other stands for object after "<" sign. (so t is self and t2 other)
		"""
        return self.x < other.x	# Should be used on objects of the same type

t = Test(5)
t2 = Test(2)
print(t < t2)	# Now I call the __lt__ method by using "<" sign.
Comment

PREVIOUS NEXT
Code Example
Python :: missing data in python 
Python :: how to plot stacked bar chart from grouped data pandas 
Python :: map a list to another list python 
Python :: python create gif 
Python :: how to check a string is empty in python 
Python :: excel write column 
Python :: python function with infinite parameters 
Python :: python sort 
Python :: matrix diagonal sum leetcode in Python 
Python :: pandas reset index start from 0 
Python :: python get file line count 
Python :: sphinx autodoc command 
Python :: python how to remove n from string 
Python :: Aggregate on the entire DataFrame without group 
Python :: getting multiple of 5 python 
Python :: how to make a superuser in django 
Python :: python update header row 
Python :: ImportError: No module named _bootlocale 
Python :: numpy reshape (n ) to (n 1) 
Python :: python combinations 
Python :: python encode file 
Python :: get first letter of each word in string python 
Python :: list to dataframe pyspark 
Python :: dockerfile to run python script 
Python :: python check if ip is up or down 
Python :: print data type array 
Python :: x y coordinates in python 
Python :: session in django 
Python :: python get attribute value with name 
Python :: Python format() Method for Formatting Strings 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =