Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Python how to use __gt__

"""
Magic method __gt__ stands for greater than. So this magic method defines or 
overwrites what should ">" sign do with objects.
"""

class Test:
    def __init__(self, x):
        self.x = x
    
    def __gt__(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 __gt__ method by using ">" sign.
Comment

PREVIOUS NEXT
Code Example
Python :: python extract value from a list of dictionaries 
Python :: array as an input in python 
Python :: if keyboard.is_pressed 
Python :: How to Convert Strings to Datetime in Pandas DataFrame 
Python :: print () 
Python :: how to randomize order of a list python 
Python :: drop duplicate rows pandas except nan 
Python :: python close browser 
Python :: python program running time 
Python :: colab add package 
Python :: python make dictionary based on list 
Python :: generate gif py 
Python :: download a file from url python 
Python :: cast tensor type pytorch 
Python :: boto3 upload file to s3 
Python :: simple trivia question python 
Python :: im save to a bytes io python 
Python :: export csv 
Python :: python async await 
Python :: modulus of python complex number 
Python :: how to create obtain any random 3 items of list in python 
Python :: making a function wait in python 
Python :: how to check if file exists pyuthon 
Python :: tkinter button command with arguments 
Python :: jupyter nbconvert 
Python :: python set remove 
Python :: tkinter open new window 
Python :: how to add column to np array 
Python :: append record in csv 
Python :: case statement in querset django 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =