Python __instancecheck__() Magic Method

Syntax class.__instancecheck__(self, instance) Python’s class.__instancecheck__(self, instance) method implements the isinstance(instance, class) built-in function. It should return True if instance is a direct or indirect instantiated object of class (e.g., via Python inheritance). We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with … Read more

Python __subclasscheck__() Magic Method

Syntax class.__subclasscheck__(self, subclass) Python’s class.__subclasscheck__(self, subclass) method implements the issubclass(subclass, class) built-in function. It should return True if subclass is a direct or indirect subclass of class using Python inheritance to define the subclass relationship. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all … Read more

Python __sizeof__() Method

Syntax object.__sizeof__(self, other) The Python __sizeof__() method returns the size of the object in bytes. The sys.getsizeof() method internally call’s __sizeof__() and adds some additional byte overhead, e.g., for garbage collection. Basic Example The following code snippet creates a list object x with three integers and measures its size in bytes (104) using the x.__sizeof__() … Read more

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