Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Python how to use __le__

"""
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 how to use __sub__ 
Python :: __truediv__ 
Python :: Python how to use __le__ 
Python :: create different size matplotlib 
Python :: NumPy resize Example out of bound values [appending zeros] 
Python :: python mxs Classof 
Python :: how to nest try/except statements 
Python :: change bg awesomewm 
Python :: NumPy bitwise_xor Code When inputs are Boolean 
Python :: All possible combinations of multiple columns 
Python :: should either include a `queryset` attribute, 
Python :: python override inherited method data model constructor 
Python :: mock connection sqlalchemy 
Python :: get primary key in get_context_data 
Python :: bouton 
Python :: manager.dict() append 
Python :: send message in every channel discord.py 
Python :: python turtle star 
Python :: heatmap colorbar label 
Python :: if no python 
Python :: plot bar 
Python :: typing effect python 
Python :: python complement operator 
Python :: ring convert string lines to list items and convert the list to a string 
Python :: z3 symbolic expressions cannot be cast to concrete boolean values 
Python :: get correlation between two signals 1d scipy 
Python :: talib 
Python :: We want to estimate the cost of painting a property. Interior wall painting cost is Rs.18 per sq.ft. and exterior wall painting cost is Rs.12 per sq.ft. 
Python :: ax text relative coordinates 
Python :: python print string in red color 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =