Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Python __gt__ magic method

"""
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 palindrome string 
Python :: compute eigenvalue python 
Python :: find the area of a circle in python 
Python :: update print python 
Python :: how to print hello world in python 
Python :: python cmath constants 
Python :: accessing data on django sessionstore 
Python :: fibonacci sequence python 
Python :: printing python dictionary values 
Python :: while loop user input python 
Python :: python style console output 
Python :: flask redirect to url 
Python :: pytest loop 
Python :: how to remove blank lines from string in python 
Python :: how to use variables in string in python 
Python :: how many days until 2021 
Python :: Inheritance constructor with parameters python 
Python :: errno 13 permission denied python 
Python :: python bash command 
Python :: python screen click 
Python :: correlation between two columns pandas 
Python :: bs4 python delete element 
Python :: change default option optionmenu tkinter 
Python :: Efficiently count zero elements in numpy array? 
Python :: how to execute python program in ubuntu 
Python :: sklearn train_test_split 
Python :: 3d array in numpy 
Python :: python jokes 
Python :: python append to csv on new line 
Python :: python fill 0 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =