Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Python __le__ magic method

"""
Magic method __le__ stands for lesser than or equal. So this magic method defines or 
overwrites what should "<=" sign do with objects.
"""

class Test:
    def __init__(self, x):
        self.x = x
    
    def __le__(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(2)
t2 = Test(2)
print(t <= t2)	# Now I call the __le__ method by using "<=" sign.
Comment

PREVIOUS NEXT
Code Example
Python :: python interpreter after running a python file 
Python :: django ejemplo de un formulario crud 
Python :: python cos not the same as calculator 
Python :: NumPy rot90 Example Rotating Twice 
Python :: p0, percent, aug (inhabitants coming or leaving each year), p (population to surpass) 
Python :: del mutiple indexes at once 
Python :: change bg awesomewm 
Python :: NumPy bitwise_xor Code When inputs are numbers 
Python :: Convertion of number into binary using NumPy binary_repr 
Python :: tikzplotlib set figure 
Python :: using .get() for deep dictionary 
Python :: instance variables python 
Python :: penggunaan fromkeys di python 
Python :: Creating a Nested Dictionary 
Python :: python special methods list 
Python :: Use one function for the "ComboboxSelected", to read multiple combobox 
Python :: find smallest element not present in list python 
Python :: operasi tipe data integer 
Python :: multiply each element by x in python 
Python :: how to show type of a variable 
Python :: if space bar pressed pygame 
Python :: how to import grades into a text file in python 
Python :: perform cross tabulation sklearn 
Python :: ring Using Self.Attribute and Self.Method 
Python :: python graph 
Python :: importing cosine from scipy 
Python :: import sys execute cmd 
Python :: limiting a for each loop python 
Python :: python apply file line 
Python :: default python structure 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =