Python __floordiv__() Magic Method

Syntax object.__floordiv__(self, other) 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 … Read more

Python __truediv__() Magic Method

Syntax object.__truediv__(self, other) The Python __truediv__() method is called to implement the normal division operation / called true division—as opposed to the floor division operation //. For example to evaluate the expression x / y, Python attempts to call x.__truediv__(y). We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To … Read more

Python __matmul__() Magic Method

Syntax object.__matmul__(self, other) The Python __matmul__() method is called to implement the matrix multiplication operation @. For example to evaluate the expression x @ y, Python attempts to call x.__matmul__(y). We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check … Read more

Python __mul__() Magic Method

Syntax object.__mul__(self, other) 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). We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check … Read more

Python __sub__() Magic Method

Syntax object.__sub__(self, other) 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. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat … Read more

Python __add__() Magic Method

Syntax object.__add__(self, other) Python’s object.__add__(self, other) method returns a new object that represents the sum of two objects. It implements the addition operator + in Python. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat … Read more

Python __abs__()

Syntax object.__abs__() Python’s object.__abs__() method returns the absolute value of the object and implements the built-in function abs(). The absolute value of any numerical input argument -x or +x is the corresponding positive value +x. However, you can overwrite the default behavior by defining a custom __abs__() method. We call this a “Dunder Method” for … Read more