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