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 :: dataframe row 
Python :: media django 
Python :: delete unnamed coloumns in pandas 
Python :: convert mb to gb python 
Python :: get file names in folder python 
Python :: python read requests response 
Python :: python telegram bot send image 
Python :: telnet python 
Python :: python cv2.Canny() 
Python :: python count hex 
Python :: simple colours python 
Python :: how to show pandas last record 
Python :: check object attributes python 
Python :: instagram private account hacking code python 
Python :: how to access variable from another function in same class in python 
Python :: python ceil 
Python :: sklearn cross validation score 
Python :: extract month as integer python 
Python :: python append element to array 
Python :: python program to find factorial 
Python :: how to copy text file items to another text file python 
Python :: how to import pygame 
Python :: while loop user input python 
Python :: flask send client to another web page 
Python :: django post request 403 forbidden 
Python :: django create token for user 
Python :: Creating a list with list comprehensions 
Python :: length of a matrix in python 
Python :: numpy generate random 2d array 
Python :: how to sort dictionary in python by lambda 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =