Search
 
SCRIPT & CODE EXAMPLE
 

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 :: convert float to string python 
Python :: datetime.timedelta format to string python 
Python :: restart python after script execution 
Python :: python delete elements from list / range 
Python :: numpy random 
Python :: python find if strings are anagrams 
Python :: python tkinter dynamic toggle button 
Python :: python string interpolation 
Python :: matrix diagonal sum python 
Python :: how to write a comment in python 
Python :: python number of lines in file 
Python :: hide turtle 
Python :: change markersize in legend matplotlib 
Python :: dataframe print column 
Python :: sort values within groups pandas dataframe 
Python :: django create super user 
Python :: WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. 
Python :: modify a list with for loop function in python 
Python :: reverse python dictionary 
Python :: pandas fillna with none 
Python :: matplotlib: use colormaps for line plot colors 
Python :: Identify Null and NAN python 
Python :: convert python list to pyspark column 
Python :: python empty dataframe 
Python :: django save object in view 
Python :: stacks in python 
Python :: plot matrix as heatmap 
Python :: how to get session value in django template 
Python :: How to Connect Google Colab to a Local Jupyter Runtime 
Python :: python not in list 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =