Python __aiter__() and __anext__() Magic Methods

object.__aiter__(self) object.__anext__(self) 💡 Summary: Python’s __aiter__() and __anext__() methods are used to implement an asynchronous for loop (keywords: async for). In contrast to a normal (synchronous) for loop, an asynchronous for loop iterates over an asynchronous source. __aiter__() returns an asynchronous iterator object (in many cases it’s simply a reference to itself: return self) __anext__() … Read more

Python __aexit__() Magic Method

object.__aexit__(self, exc_type, exc_val, exc_tb) 💡 Summary: Python’s __aexit__() magic method is semantically similar to __exit__() but is used for asynchronous and parallel programming. Python calls the __aexit__() magic method when leaving an async with block whereas the __aenter__() method is called when entering it. An object that implements both __aenter__() and __aexit__() methods is called … Read more

Python __aenter__() Magic Method

object.__aenter__(self) 💡 Summary: Python’s __aenter__() magic method is semantically identical to __enter__() but is used for asynchronous and parallel programming. Python calls the __aenter__() magic method when starting an async with block whereas the __aexit__() method is called when leaving it. An object that implements both __aenter__() and __aexit__() methods is called an asynchronous context … Read more

Python __exit__() Magic Method

object.__exit__(self, exc_type, exc_value, traceback) 💡 Summary: Python calls the __exit__() magic method when ending a with block whereas the __enter__() method is called at the start. An object that implements both __exit__() and __enter__() is called a context manager. By defining those methods, you can create your own context manager. We define a custom class … Read more

Python __enter__() Magic Method

object.__enter__(self) 💡 Summary: Python calls the __enter__() magic method when starting a with block whereas the __exit__() method is called at the end. An object that implements both __enter__() and __exit__() methods is called a context manager. By defining those methods, you can create your own context manager. We define a custom class MySecretConnection. This … Read more

Python __set__ Magic Method

Python’s __set__() magic method sets a given attribute on an instance of a class holding the attribute (=owner class) to a new value. When setting the attribute through the owner class, Python dynamically executes the attribute’s __set__() method to override its value on the given instance argument. For example, if you create a class Person … Read more

How to Find All the Subclasses of a Class?

Problem Formulation Given a class by name (string). How to find all the subclasses of the given class? Example: Here’s an example scenario with a subclass hierarchy. Desired outputs: Next, let’s quickly establish what you want to accomplish with two examples. Given: Son Result: Grandson Given: Parent Result: Daughter, Son For the most basic cases, … Read more

Python __get__ Magic Method

Python’s __get__() magic method defines the dynamic return value when accessing a specific instance and class attribute. It is defined in the attribute’s class and not in the class holding the attribute (= the owner class). More specifically, when accessing the attribute through the owner class, Python dynamically executes the attribute’s __get__() method to obtain … Read more

Python __init_subclass__() Magic Method

The Python class.__init_subclass__(cls) method is called on a given class each time a subclass cls for that class is created. Syntax class.__init_subclass__(cls) We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this … Read more

Python __getattribute__() Magic Method

Python’s magic method __getattribute__() implements the built-in getattr() function that returns the value associated with a given attribute name. If the __getattribute__() error results in an AttributeError due to a non-existent attribute, Python will call the __getattr__() function for resolution. Thus, the __getattribute__() method takes precedence over the __getattr__() method. We call this a “Dunder … Read more

Python __del__() Magic Method

Python’s magic method __del__() is called the finalizer method or, wrongly, the destructor method — the latter being wrong because it doesn’t actually destroy the object. Python calls __del__() upon deletion of a given instance. For example, the expression del my_obj will eventually initiate my_obj.__del__(). We call this a “Dunder Method” for “Double Underscore Method” … Read more

Python __delete__() Magic Method

Python’s magic method __delete__() is called to delete an instance’s attribute. For example, the expression del my_obj.attr would result in attr.__delete__(my_obj), so you’d give the attribute itself the responsibility for its deletion on my_obj. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder … Read more