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

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

Finxter Feedback from ~1000 Python Developers

This feedback is the collective feedback of a year’s worth of teaching hundreds of thousands of coders in our email program. Here’s the quick overview chart of all 955 responses: In case you want to join the email academy, check it out here: Let’s get started with the unfiltered list of feedback! List of 1000 … 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 __await__() Magic Method

The __await__() magic method defines the behavior of an awaitable object. An awaitable object is used to implement asynchronous behavior in Python. For example, you can implement an asynchronous function that waits for the data based to access some data like so (see source): Syntax __await__() object.__await__(self) The __await__() method must return an iterator. Otherwise, … Read more

Python __annotations__ Attribute

Annotations are defined in PEP 3107 allow you to add arbitrary metadata to the parameters and return values of functions. The __annotations__ attribute of a function object stores those annotations in a dictionary mapping function parameters or the return value to the specified annotations. Let’s have a look at a couple of examples next. Parameter … Read more