How to Override the “not” Operator in a Python Magic Method?

Short Answer: To override the logical not operator for a custom Python class My_Class, redefine the dunder method My_Class.__bool__() to return your custom Boolean value. This ensures that bool(x) on a My_Class object x returns either True or False. The operation not x will then return the inverse Boolean value, i.e, not x evaluates to … Read more

Python __iter__() Magic Method

Syntax object.__iter__(self) The Python __iter__ method returns an iterator object. An iterator object is an object that implements the __next__() dunder method that returns the next element of the iterable object and raises a StopIteration error if the iteration is done. Formally, the __iter__() method implements the built-in iter() function. For example, if you call … Read more

Python __next__() Magic Method

Syntax object.__next__(self) The Python __next__ method returns an arbitrary element that represents the “next” element when you iterate over the object on which it is called. For example, if you iterate over my_object using for x in my_object, Python internally calls my_object.__next__() in each loop iteration to determine the next element. Formally, the __next__() method … Read more

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