Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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.
 
PREVIOUS NEXT
Tagged: #Python #magic #method
ADD COMMENT
Topic
Name
8+5 =