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

dir() versus __dir__() – What’s the Difference?

Problem Formulation What’s the difference between the built-in dir() function and __dir__ dunder method in Python? Quick Answer Python’s built-in function dir(object) returns a list of the object’s attribute names and method names. The dir() function is a wrapper around the __dir__() method because it internally calls the object’s dunder method object.__dir__(). But the two … Read more

Python List of Dunder Methods

When searching for a list of dunder 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: __abs__ __add__ __aenter__ __aexit__ __aiter__ __and__ __anext__ __await__ __bool__ __bytes__ __call__ __ceil__ … Read more

How to Serialize a Python Dict into a String and Back?

Problem Formulation Given a Python dictionary containing lists and other data structures. You want to store the dictionary in a file or send it over the network in a more efficient form. How to serialize a Python dictionary into a string, and then deserialize the string back to a dictionary data structure? Here’s a rough … Read more

[Solved] AttributeError: can’t set attribute in python

Problem Formulation You create a namedtuple object in your code and you want to overwrite one of the attribute values like you’re used to for normal objects: But, unexpectedly, Python raises an AttributeError: can’t set attribute. ? What can you do? ? Let’s understand the reason this error occurs—after that, you’ll learn the best fixes. … Read more