Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Python __lt__ magic method

"""
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 :: cannot reshape array of size 2137674 into shape (1024,512,3,3) 
Python :: how to remove role from people with a reaction discord bot python 
Python :: dictionary in python 
Python :: python sort an array 
Python :: matplotlib 
Python :: boto3.client python 
Python :: python tkinter button dynamic button command 
Python :: how to convert string to integer in python 
Python :: leetcode matrix diagonal sum in python 
Python :: how to write a python comment 
Python :: Python using webbrowser 
Python :: slack bot error not_in_channel 
Python :: Python: Extracting XML to DataFrame (Pandas) 
Python :: iterate over a list python 
Python :: python how to extract a string from another string 
Python :: Math Module pow() Function in python 
Python :: string to list python 
Python :: python selenium print element 
Python :: close a file python 
Python :: dict in dict in python 
Python :: upload bytes to s3 python 
Python :: doc strings python 
Python :: install python macos catalina 
Python :: python string encode 
Python :: pandas cummin 
Python :: black code formatter 
Python :: pandas make dataframe from few colums 
Python :: array with zeros python 
Python :: catch exception python unittest 
Python :: iterrrows 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =