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

Python __int__() Magic Method

Syntax object.__int__(x) The Python __int__() method implements the built-in int() function. So, when you call int(x), Python attempts to call x.__int__(). If the return value is not an integer, Python will raise a TypeError. If it’s not implemented, Python attempts to call x.__index__() instead, and only if this is not implemented either, it raises a … Read more

Python __float__() Magic Method

Syntax object.__float__(x) The Python __float__() method implements the built-in float() function. So, when you call float(x), Python attempts to call x.__float__(). If the return value is not a float, Python will raise a TypeError. If x.__float__() is not implemented, Python attempts to call x.__index__() first and only if this is not implemented either, it raises … Read more

Python __index__() Magic Method

Python’s __index__(self) method is called on an object to get its associated integer value. The returned integer is used in slicing or as the basis for the conversion in the built-in functions bin(), hex(), and oct(). The __index__() method is also used as a fallback for int(), float(), and complex() functions when their corresponding magic … Read more

No, Python __oct__() Doesn’t Exist Anymore. Do This Instead!

The Problem TypeError: ‘…’ object cannot be interpreted as an integer If you’re reading this article, chances are that you have been thinking something along those lines: Given a custom class My_Class. You want to override the behavior of the built-in oct(x) function in Python when calling it on a My_Class object x. You know … Read more

No, Python __hex__() Does Not Exist. Do This Instead!

The Problem TypeError: ‘…’ object cannot be interpreted as an integer If you’re reading this article, chances are that you have been thinking something along those lines: Given a custom class My_Class. You want to override the behavior of the built-in hex(x) function in Python when calling it on a My_Class object x. You know … Read more

Python __hash__() Magic Method

Syntax object.__hash__(x) The Python __hash__() method implements the built-in hash() function. So, when you cal hash(x), Python attempts to call x.__hash__(). If the return value is not an integer or the x.__hash__() method is not defined, Python will raise a TypeError. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). … Read more

Python __complex__() Magic Method

Syntax object.__complex__(x) The Python __complex__() method implements the built-in complex() function. So, when you cal complex(x), Python attempts to call x.__complex__(). If the return value is not a complex number or the x.__complex__() method is not defined for an object on which you call complex(x), Python will raise a TypeError. We call this a “Dunder … Read more

Python __bytes__() Magic Method

Syntax object.__bytes__(self) The Python __bytes__() method implements the built-in bytes() function. So, when you cal bytes(x), Python attempts to call x.__bytes__(). If the return value is not a Bytes object or the x.__bytes__() method is not defined for an object on which you call bytes(x), Python will raise a TypeError. We call this a “Dunder … Read more

Python __bool__() Magic Method

Syntax object.__bool__(self) The Python __bool__() method implements the built-in bool() function. So, when you cal bool(x), Python attempts to call x.__bool__(). If the return value is not a Boolean, Python raises a TypeError. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods … Read more

Python __ixor__() Magic Method

Syntax object.__ixor__(self, other) The Python __ixor__() magic method implements the in-place bitwise XOR x ^= y that calculates the result of the bitwise XOR operation x ^ y, and assigns it to the first operands’ variable x. This type of in-place operation is also called augmented arithmetic assignment. The method simply returns the new value … Read more