Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Python how to use __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 :: how to find the longest string python 
Python :: how to remove role discord bot python 
Python :: scikit learn to identify highly correlated features 
Python :: raise_for_status() requests 
Python :: python for dummies 
Python :: Group by a column, count sum of other columns 
Python :: how to check if a variable holds a class reference in python 
Python :: how to convert a datatype to another 
Python :: matrix diagonal sum leetcode in java 
Python :: how to comment text in python 
Python :: how to merge two column pandas 
Python :: uninstall every package in environment 
Python :: python min key 
Python :: logical operators python 
Python :: python how to add a string to a list in the middle 
Python :: loads function in json python 
Python :: pandas dataframe from list how to make the date column an index 
Python :: convert string to int dataframe column 
Python :: openmp for loop 
Python :: method get first last name python 
Python :: count number of objects django template 
Python :: python fill string with spaces to length 
Python :: pandas read csv python 
Python :: pandas split column into multiple columns 
Python :: #adding new str to set in python 
Python :: defaultdict python dict inside dict 
Python :: print to file python 
Python :: class inside class python 
Python :: cholesky decomposition in python 
Python :: regex find all french phone number python 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =