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 __format__() Magic Method

Syntax object.__format__(self, spec) The Python __format__() method implements the built-in format() function as well as the string.format() method. So, when you call format(x, spec) or string.format(spec), Python attempts to call x.__format__(spec). The return value is a string. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list … Read more

Python __set_name__() Magic Method

Python’s magic method obj.__set_name__(self, owner, name) method is created on an attribute obj when the class owner holding the attribute is created. 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 __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 __getitem__() Magic Method

Python’s magic method __getitem__(self, key) to evaluate the expression self[key]. So, if you call my_obj[key], Python will call my_obj.__getitem__(key). 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 blog. Syntax and … Read more

Python __setitem__() Magic Method

Python’s magic method __setitem__(self, key, value) implements the assignment operation to self[key]. So, if you call self[key] = value, Python will call self.__setitem__(key, value). 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 … Read more

Python __delitem__() Magic Method

Python’s magic method __delitem__(self, key) implements the deletion of self[key]. So, if you call del self[key], Python will call self.__delitem__(key). 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 blog. Syntax … Read more