Python __ceil__() Magic Method

Syntax and Description object.__ceil__(self) The Python __ceil__() method implements the behavior of the math.ceil() function. For example, if you attempt to call math.ceil(x), Python will run the x.__ceil__() method to obtain the return value. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder … Read more

Python __round__() Magic Method

Syntax object.__round__(self, ndigits=0) The Python __round__() method implements the built-in round() function. For example, if you attempt to call round(x) or round(x, ndigits), Python will run the x.__round__() or x.__round__(ndigits) method, respectively. The following code snippet overrides the __round__() dunder method to return the rounded age of a Person when you pass an object of … Read more

Python __rdiv__ Magic Method

The Python __rdiv__() magic method overrides the reverse division operation for a custom object in Python 2. In Python 3, it was replaced by the __rtruediv__() and __rfloordiv__() dunder methods. The Python __rtruediv__() method is called to implement the normal division operation / called true division and apply it in reverse. The Python __rfloordiv__() method … Read more

Python __neg__ Magic Method

To customize the behavior of the negation operator -x, override the __neg__(self) dunder method in your class definition. Python internally calls x.__neg__() to calculate the inverse (negation) of an object, i.e., -x. If the __neg__() method is not defined, Python will raise a TypeError. Syntax __neg__(self) To use the negation operator -x on a custom … Read more

Pandas DataFrame Comparison Operators and Combine – Part 3

The Pandas DataFrame has several binary operator methods. When applied to a DataFrame, these methods combine two DataFrames and return a new DataFrame with the appropriate result. This is Part 3 of the following series on Pandas DataFrame operators: Part 1: Pandas DataFrame Arithmetic Operators Part 2: Pandas DataFrame Reverse Methods Part 3: Pandas DataFrame … Read more

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 __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

How to Make Division by Zero to Zero in Python?

In Python, division by zero generates the exception ZeroDivisionError:  division by zero.  This is because in mathematics, division by zero is undefined. Today we’ll go over some ways to avoid the zero division error in Python and force it to return a zero. First, we’ll look into exception handling in Python. Second, we’ll examine different … 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