[Python] Magic Method

[Python] Magic Method

What is a magic method?

Magic methods, also known as dunder methods (short for "double underscore"), are special methods in Python that start and end with double underscores. They automatically invoked by python to perform specific operations.

Example:

  • __init__

  • __add__

  • __str__

  • __repr__


Why is it important?

We can get the calculation result of 1 + 1 by using + operand in python, but have you ever thought of how 1 + 1 (or any other calucations) is interpreted by python? Let’s have a look at this code:

a = 1
print(type(a)) # <class 'int'>

We assign 1 to a, so a is an integer. type(a) tells us that the type of a is ‘int’, but you need to have a look at ‘class’. int is a class. As int is a class, float and str are also classes.

Let’s check the attributes and methods that we can use! The dir() function in Python is used to list the attributes and methods of an object. It returns all properties and methods of the specified object, without the values.

a = 1
print(type(a)) # <class 'int'>
print(dir(a))

Result of print(dir(a)):

The second element of the list is ‘__add__’. Now, let me show you using a magic method.

a = 10
print(a + 10) # 20
print(a.__add__(10)) # 20

The result of using + operand and using the __add__ are the same. I am not gonna lie, + operand is way easier to understand and more readable. Then, why does python have so many magic methods? It’s because it allows us to customize the method. + operand only allows us to add two numbers, which is not fun and flexible.


Powerfulness of Magic Method

I am going to introduce you some examples to show you how powerful magic methods are.

Example:

class Fruit():
    def __init__(self, name, price):
        self._name = name
        self._price = price

    def __str__(self):
        return f'Fruit Class Info: {self._name}, {self._price}'

    def __add__(self, x):
        print('Called >> __add__')
        return (self._price + x._price) * 0.8

s1 = Fruit('Orange', 7500)
s2 = Fruit('Banana', 3000)

# Not using magic method
print(s1._price + s2._price)

# Using magic method
print(s1 + s2)

Notice that I did (self._price + x._price) * 0.8. I am not just adding two values, but also multiplying it by 0.8. Let’s say you are the owner of a fruit store, and you want to set the sales percentage differently every month. Then, you can just addit __add__ magic method in your Fruit() class. How powerful!

Did you find this article valuable?

Support Jay's Dev Blog by becoming a sponsor. Any amount is appreciated!