Python Magic Methods

To add "magic" to the class we create, we can define special methods called "magic methods." For example, the magic methods __init__ and __str__are always wrapped by double underscores from both sides. By granting us accessibility to Python's built-in syntax tools, magic methods can improve the structure of our classes.

We can integrate Python's built-in classes with our classes. The class which has inherited from the built-in class is known as a child class. A child class has access to all of the attributes of the parent class, including its methods. By utilizing the essential built-in features, we can customize some of the tasks of our class by using magic methods.

__init__ Method

After we have constructed an instance of the class, but before that instance is returned to the caller of the class, the _init_ method is executed. When we create an instance of the class, it is called automatically, just like constructors in various programming languages like the popular ones C++, Java, C#, PHP, etc. These methods are invoked after _new_ and therefore are referred to as initialising. We should define the instance parameters here.

Code

Output:

Now called __init__ magic method, after tha initialised parameters
<__main__.methods object at 0x3701290>
Name, standard, and marks of the student is: 
 Itika 
 11 
 98

__new__() Method

The magic method __new__() is called implicitly by the __init__() method. The new instance returned by the __new__() method is initialised. To modify the creation of objects in a user-defined class, we must supply a modified implementation of the __new__() magic method. We need to provide the first argument as the reference to the class whose object is to be created for this static function.

Code

Output:

Creating an instance by __new__ method
Init method is called here
<__main__.Method at 0x30dfb88>

__add__ Method

We use the magic method __add__to add the class instance's attributes. Consider the scenario where object1 belongs to class Method and object2 belongs to class Method 1, both of which have the same attribute called "attribute" that stores any value passed to the class while creating the instance. If specified to add the attributes, the __add__ function implicitly adds the instances' same attributes, such as object1.attribute + object2.attribute, when the action object1 + object2 is completed.

Below is the code to show how we add two attributes of two instances of different classes without using the __add__ magic method.

Code

Output:

Attribute
 27
 27 Attribute
By using __add__ magic method the code changes to this.

Code

Output:

<__main__.Method object at 0x37470f0>
<__main__.Method_2 object at 0x376beb8>
 27 Attribute

Classes, Method and Method_1 in the script above have a property called "attribute" that stores a string. We create two instances, instance_1 and instances_2, with corresponding attributes of " Attribute" and " 27" values. The __add__ method is used to translate the action instance_1 + instance_2 into instance_1 + instance_2.attribute, which produces output ( 27 Attribute).

__repr__ Method

The class instance is represented as a string using the magic method __repr__. The __repr__ method, which produces a string in the output, is automatically called whenever we attempt to print an object of that class.

Code

Output:

Following are the values of the attributes of the class Method:
x = 4
y = 6
z = 2

__contains__ Method

The 'in' membership operator of Python implicitly calls the __contains__ method. We can use the __contains__ method to determine if an element is contained in an object's attributes. We can use this method for attributes that are containers ( such as lists, tuples, etc.).

Code

Output:

4 is contained in attribute:  True
5 is contained in attribute:  False

We have used the __contanis__ magic method in the program above to determine if a given integer is contained in the attribute "attribute". In this case, "attribute" is a list of integers. The integer 4 is present in the given list of integers passed to the class Method as an attribute. While 5 is not present in the list.

__call__ Method

When a class instance is called, the Python interpreter calls the magic method __call__. We can utilise the __call__ method to explicitly call an operation using the instance name rather than creating an additional method to carry out specific activities.

Code

Output:

7
35

__iter__ Method

For the given instance, a generator object is supplied using the __iter__ method. To benefit from the __iter__ method, we can leverage the iter() and next() methods.

Code

Output:

9
16
25
36
49
64

We have calculated the squares of the numbers in the code. For the numbers in the specified range, squares are calculated in the program above (start and stop). The __iter__ method, which generates squares of the numbers between the given range, is called when we call the function iter(Method(3, 8)). In our example, we're using the range of 3 to 8; therefore, calling the next() method will produce the results 9, 16, 25, 36, 49, 64.


Next TopicPython Tutorial