Mutable vs. Immutable Objects in Python

Overview: Mutable objects are Python objects that can be changed. Immutable objects are Python objects that cannot be changed. The difference originates from the fact the reflection of how various types of objects are actually represented in computer memory. Be aware of these differences to avoid surprising bugs in your programs. Introduction To be proficient … Read more

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