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

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