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

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

Python’s magic method __delattr__() implements the built-in delattr() 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

No Ads. 3 Hacks to Your Stress-Free Web Experience

They fund the web. Google is built on them. Facebook, Instagram, Reddit, and StackOverflow couldn’t keep the lights on without them. Even we at Finxter use them to fund our operation and create more helpful content. What am I talking about? … … ADVERTISEMENTS! 🀯😀😭 Yes, they may be needed to fund the web infrastructure. … Read more

How to Calculate the Edit Distance in Python?

Motivation Type “helo world” into your Google search bar and Google will ask you: “Did you mean: hello world”. How is this done? A simple method to detect these typos is the Levenshtein distance (also called edit distance). In fact, Google’s algorithm seems to use some variant of it. (source) By studying this article, you’ll … Read more