Duck Typing in Python

πŸ’‘ Idea: Duck typing is a computer science concept where the type of an object is largely ignored—only the methods the object defines matter. This is sometimes referred to as dynamic typing because the type of an object is determined dynamically at runtime rather than checked by the compiler. Instead of checking the type, the … 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 __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

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

Python __ior__() Magic Method

Syntax object.__ior__(self, other) The Python __ior__() magic method implements the in-place bitwise OR x |= y that calculates the result of the bitwise OR 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