Python __len__() Magic Method

Syntax object.__len__(self) The Python __len__ method returns a positive integer that represents the length of the object on which it is called. It implements the built-in len() function. For example, if you call len(x) an object x, Python internally calls x.__len__() to determine the length of object x. We call this a “Dunder Method” for … Read more

Python __str__()

Syntax object.__str__(self) The Python __str__ method returns a string representation of the object on which it is called. For example, if you call print(x) an object x, Python internally calls x.__str__() to determine the string representation of object x. This method is also used to implement the built-in str() function. We call this a “Dunder … Read more

Python __ne__ Magic Method

To customize the behavior of the non-equality operator x != y, override the __ne__() dunder method in your class definition. Python internally calls x.__ne__(y) to compare two objects using x != y. If the __ne__() method is not defined, Python will use the is not operator per default that checks for two arbitrary objects whether … Read more

Python __le__() Magic Method

Short summary: To customize the behavior of the less than or equal to operator x <= y, override the __le__() dunder method in your class definition. Python internally calls x.__le__(y) to obtain a return value when comparing two objects using x <= y. The return value can be any data type because any value can … Read more

Python __lt__() Magic Method

Short summary: To customize the behavior of the less than operator x < y, override the __lt__() dunder method in your class definition. Python internally calls x.__lt__(y) to obtain a return value when comparing two objects using x < y. The return value can be any data type because any value can automatically converted to … Read more

Python __gt__() Magic Method

Short summary: 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 using x > y. The return value can be any data type because any value can automatically converted to … Read more

Python __eq__ Magic Method

To customize the behavior of the equality operator x == y, override the __eq__() dunder method in your class definition. Python internally calls x.__eq__(y) to compare two objects using x == y. If the __eq__() method is not defined, Python will use the is operator per default that checks for two arbitrary objects whether they … Read more

Python __div__() Magic Method

The Python __div__() magic method overrides the division operation for a custom object in Python 2. In Python 3, it was replaced by the __truediv__() for a / b and __floordiv__() dunder methods for a // b. The Python __truediv__() method is called to implement the normal division operation / called true division. For example … Read more

The Sieve of Eratosthenes in Python

Finding prime numbers is of critical importance for practical applications such as cryptography. Many public-key methods are only safe from a cryptographic point of view because it’s generally inefficient and slow to compute the prime factors of large numbers. As you go over the article, feel free to watch my explainer video on the Sieve … Read more

Python __invert__() Magic Method

Syntax object.__invert__(self) The Python __invert__() method implements the unary arithmetic operation bitwise NOT ~. So, when you cal ~x, Python will internally call x.__invert__() to obtain the inverted object. If the method is not implemented, Python will raise a TypeError. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To … Read more