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

Python Dunder Methods Cheat Sheet

When searching for a list of dunder methods with explanations, i.e., a cheat sheet on Python magic methods, I only found a couple of resources all over the web—each covering only a fraction of dunder methods. So, here’s my collection of 127 dunder methods (also called “magic methods”) from those sources, sorted alphabetically. I provide … Read more