Python __contains__() Magic Method

Syntax and Definition The Python __contains__() magic method implements the membership operation, i.e., the in keyword. Semantically, the method returns True if the argument object exists in the sequence on which it is called, and False otherwise. For example, 3 in [1, 2, 3] returns True as defined by the list method [1, 2, 3].__contains__(3). … Read more

Python __repr__() Magic Method

Syntax object.__repr__(self) The Python __repr__ method returns a string representation of the object on which it is called. It implements the built-in repr() function. If you call print(x) an object x, Python internally calls x.__str__() to determine the string representation of object x. If this isn’t implemented, Python calls x.__repr__(). We call this a “Dunder … Read more

Python __dir__() Magic Method

Python’s __dir__() magic method implements the functionality of the dir() built-in function. Semantically, dir() returns all (function, object, or variable) names in a given scope. However, the magic method __dir__() converts any return value to a sorted list. Minimal Example The following code defines a custom class My_Class and overrides the __dir__() magic method to … Read more

Python __trunc__() Magic Method

Syntax and Definition object.__trunc__(self) The Python __trunc__() method implements the behavior of the math.trunc() function. For example, if you attempt to call math.trunc(x), Python will run the x.__trunc__() 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 __floor__() Magic Method

Syntax and Definition object.__floor__(self) The Python __floor__() method implements the behavior of the math.floor() function. For example, if you attempt to call math.floor(x), Python will run the x.__floor__() 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 __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