Dunder methods are extremely strong Python intrinsic methods that can be used to develop complex classes. This lesson provides a step-by-step guide of Python methods and how to use them, complete with examples. The consistency of Python’s popularity can be traced to the language’s popularity. For intrinsic methods like abs(), len(), sorted(), and many others, Python allows you to implement your custom objects. All you have to do now is add some unique methods to your class (dunder methods).
In this article, we have used phrases like dunder methods, magic methods, and special techniques, all of which fall into the same category. Before we go into the tutorial, you should be aware of something. Python programmers who are experienced with object-oriented programming (OOP) will recognize the __init__ method. The dunder method was named after the double underscores, which are known as dunder in Python. Because there are magic methods in Ruby that fulfill the same purpose, some Python programmers refer to them as such in Python. We usually refer to them as Special Methods because that’s what the Python manual calls them. Resultantly, if you see these methods in Python, assume they are Special Methods.
Example 1:
Similar to constructors in other programming languages, when an instance of a class is formed, the __init__ function for initialization is invoked without any call. In Python classes, “__init__” is a reserved method. In object-oriented terminology, it is referred to as a function Object() { [native code] }. This method is invoked when an object is formed from a class to permit the class to populate its properties. We can use the ‘+’ operator to combine two strings without needing to apply explicit typecasting because of these methods. Here is a detailed example of how to perform this. We declared our string class first, followed by the magic function to create an object. The object creation is completed after the driver code, and the object location is reported out at the end.
The code lines above merely print the string object’s memory address.
Example 2:
We’ll utilize the __repr__ method to represent our object in the following example. The magic method __repr__ represents a class instance in a string. When we try to print a class object, the __repr__ function is called, which yields a string. By default, repr() returns the string representation of the value supplied to the eval function. By default, the custom class object provides a string in angle brackets containing the object’s name and address. Except for the addition of the __repr__ method after we’ve used the magic method to initialize the object, the code is the same as before.
Here you can see that the above have successfully displayed the string.
Example 3:
We’ll now add the __add__ function to the String class. The class instance’s attributes are added using the __add__ magic method. Let’s pretend that object1 belongs to class A and object2 to class B and that both of these classes have an attribute named ‘a’ that contains an integer. After the operation object1 + object2 is done, the __add__ function implicitly adds the attributes of these objects, such as object1.a + object2.a. We re-initialized the string class in the code, as you can see in the first line of code. After that, we utilized the magic way to get the object started. The string object was then printed using the __add__ method. The object is created after the driver code, which is the next line of code. In the last line of code, we concatenated a String object with a string.
class String:
Below is the output of the above-written code.
Conclusion:
Magic methods are Python methods with double underscores at the beginning and conclusion. Dunder methods or techniques are another name for them. Magic methods are not intended to be called directly by the user; instead, they are called internally by the class in reaction to a specific action. The __add__() method is called internally when you use the + operator to add two numbers, for example. Python’s built-in classes define a lot of magic methods. To find out how many magic methods a class has inherited, you can use the dir() function.