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 … 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

Python __getattr__() Magic Method

Python’s magic method __getattr__() implements the built-in getattr() function that returns the value associated with a given attribute name. Additionally, __getattr__() is called if the normal attribute access (e.g., my_object.my_attribute) results in an AttributeError. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder … Read more

Python __setattr__() Magic Method

Python’s magic method __setattr__() implements the built-in setattr() function that takes an object and an attribute name as arguments and removes the attribute from the object. 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 … Read more

Python Special Attributes

Python has multiple special attributes that are defined per default for each class such as __name__, __module__, __dict__, __bases__, __doc__, and __annotations__. Each of these special attributes has a special meaning as shown in the following table: Attribute Type Description __name__ str The name of the class __module__ str The string name of the module … Read more

Python __new__ Magic Method

Python’s __new__(cls) magic method creates a new instance of class cls. The remaining arguments are passed to the object constructor. The return value is the newly-created object—an instance of cls. Basic Example The following example shows how each time you create an object of our custom class My_Class, Python calls the __new__() magic method. Output: … Read more

Python __missing__() Magic Method

Syntax object.__missing__(self, key) The __missing__(self, key) method defines the behavior of a dictionary subclass if you access a non-existent key. More specifically, Python’s __getitem__() dictionary method internally calls the __missing__() method if the key doesn’t exist. The return value of __missing__() is the value to be returned when trying to access a non-existent key. We … Read more

Python __import__() Magic Method

🛑 Overriding this function is strongly discouraged. To change the semantics of the import statement, use import hooks instead! Still here? 😉 So, let’s get started learning the syntax of this function. You can also check out our in-depth article on the __import__ statement here. Syntax __import__(name, globals=None, locals=None, fromlist=(), level=0) Parameter Description name Import … Read more

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