
__str__ function :
The __str__ method is a special method in Python classes that is used to define a string representation of an object. The method should return a string that provides a human-readable representation of the object’s state. This string is used when the object is printed, or when the built-in str() function is called on the object.
If the __str__ method is not defined for a class, the interpreter will use the default implementation, which returns the name of the object’s class and its memory address.
Example:
class Robot1:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return (f"the value is {self.x} and {self.y}")
obj1 = Robot1(3, 4)
print(obj1) #output: the value is 3 and 4
__eq__ function:
The __eq__ method in Python is used to define how objects of a class are compared for equality. The __eq__ method takes two arguments: self(object on the left-hand side of == operator ) and other(object on the right-hand side of == operator). __eq__ method always returns a boolean value(True or False).
Example:
class Robot1:
def __init__(self, x, y):
self.x = x
self.y = y
def __eq__(self, other_obj):
return self.x == other_obj.x and self.y == other_obj.y
obj1 = Robot1(2, 6)
obj2 = Robot1(3, 4)
print("equal", obj1 == obj2) # True
__lt__ function:
Python __lt__ magic method is onemagic method that is used to define or implement the functionality of the less than operator “<” , it returns a boolean value according to the condition i.e. it returns true if a<b where a and b are the objects of the class.
Example:
class Robot1:
def __init__(self, x, y):
self.x = x
self.y = y
def __lt__(self, other_obj):
return self.x < other_obj.x and self.y < other_obj.y
obj1 = Robot1(2, 6)
obj2 = Robot1(3, 4)
print("lt", obj1 < obj2)
__gt__ function:
- To customize the behavior of the greather than operator
x > y, override the__gt__()dunder method in your class definition. - Python internally calls
x.__gt__(y)to obtain a return value when comparing two objects usingx > y. - The return value can be any data type because any value can automatically converted to a Boolean by using the
bool()built-in function. - If the
__gt__()method is not defined, Python will raise aTypeError.
Example:
class Robot1:
def __init__(self, x, y):
self.x = x
self.y = y
def __gt__(self, other_obj):
return self.x > other_obj.x and self.y > other_obj.y
obj1 = Robot1(2, 6)
obj2 = Robot1(3, 4)
print("gt", obj1 > obj2)
__add__ function:
The __add__() method in Python specifies what happens when you call + on two objects. When you call obj1 + obj2, you are essentially calling obj1.__add__(obj2).
Syntax:
object.__add__(self, other)
Example:
class Robot1:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other_obj):
# implementation of the addition operation
return (f"the addition value of x is {self.x + other_obj.x} and y is {self.y + other_obj.y}")
obj1 = Robot1(2, 6)
obj2 = Robot1(3, 4)
print("add", obj1+obj2) # add the addition value of x is 5 and y is 10
__sub__ function:
Python’s object.__sub__(self, other) method returns a new object that represents the difference of two objects. It implements the subtraction operator - in Python.
Syntax:
object.__sub__(self, other)
Example:
class Robot1:
def __init__(self, x, y):
self.x = x
self.y = y
def __sub__(self, other_obj):
# implementation of the addition operation
return (f"the addition value of x is {self.x - other_obj.x} and y is {self.y - other_obj.y}")
obj1 = Robot1(2, 6)
obj2 = Robot1(3, 4)
print("sub", obj1-obj2) # sub the addition value of x is -1 and y is 2
__mul__ function:
The Python __mul__() method is called to implement the arithmetic multiplication operation *. For example to evaluate the expression x * y, Python attempts to call x.__mul__(y).
Syntax:
object.__mul__(self, other)
Example:
class Robot1:
def __init__(self, x, y):
self.x = x
self.y = y
def __mul__(self, other_obj):
# implementation of the addition operation
return (f"the addition value of x is {self.x * other_obj.x} and y is {self.y * other_obj.y}")
obj1 = Robot1(2, 6)
obj2 = Robot1(3, 4)
print("mul", obj1*obj2) # mul the addition value of x is 6 and y is 24
__floordiv__ function:
The Python __floordiv__() method implements the integer division operation // called floor division—as opposed to the true division operation /. For example to evaluate the expression x // y, Python attempts to call x.__floordiv__(y). If the method is not implemented, Python first attempts to call __rfloordiv__ on the right operand and if this isn’t implemented either, it raises a TypeError.
Syntax:
object.__floordiv__(self, other)
Example:
class Robot1:
def __init__(self, x, y):
self.x = x
self.y = y
def __floordiv__(self, other_obj):
# implementation of the addition operation
return (f"the addition value of x is {self.x // other_obj.x} and y is {self.y // other_obj.y}")
obj1 = Robot1(2, 6)
obj2 = Robot1(3, 4)
print("floordiv", obj1//obj2)# floordiv the addition value of x is 0 and y is 1
Reference:
https://naiveskill.com/__str__-python/
https://python-course.eu/oop/magic-methods.php
https://www.geeksforgeeks.org/python-__lt__-magic-method/
https://www.codingem.com/python-__add__-method/
https://python.plainenglish.io/learn-how-to-use-gt-method-in-python-834e0b6bcd10
https://blog.finxter.com/python-__gt__-magic-method/
Leave a comment