Modifying the __add__ method of a Python Class

Learn how you should modify the __add__ method of a Python class to be able to add two instances of a custom object. We also talk about the __radd__ method, necessary to sum a list of instances.

When we sum two objects in Python, likeadd_custom

a + b

what really happens is that the __add__ method of the a object is called:

a.__add__(b)

Which means that we can control the result of a sum of two objects by modifying or defying the __add__ method.

For example, imagine that we define a Day class, which contains the visits and contacts that a web page generates during a day:

class Day(object):
    def __init__(self, visits, contacts):
        self.visits = visits
        self.contacts = contacts
    def __str__(self):
        return “Visits: %i, Contacts: %i” % (self.visits, self.contacts)

and we create two instances of this object:

day1 = Day(10, 1)
day2 = Day(20, 2)

so that

>>> print day1
Visits: 10, Contacts: 1
>>> print day2
Visits: 20, Contacts: 2

We can define the __add__ method to return a Day instance with the total number of visits and contacts:

class Day(object):
    …
    def __add__(self, other):
        total_visits = self.visits + other.visits
        total_contacts = self.contacts + other.contacts
        return Day(total_visits, total_contacts)
    …

and now we can just sum the two instances day1 and day2:

>>> day3 = day1 + day2
>>> print day3
Visits: 30, Contacts: 3

However, if we try:

day4 = sum([day1, day2, day3])

you’ll get an error like

TypeError: unsupported operand type(s) for +: ‘int’ and ‘Day’

This is because the sum method starts with the integer 0, and it tries:

0.__add__(day1)

However, the __add__ method of an integer doesn’t know anything about how to sum a Day instance. Therefore, it tries to call the reverse add method:

day1.__radd__(0)

which is not yet defined! So let’s do that! 😉

class Day(object):
    …
    def __radd__(self, other):
        if other == 0:
            return self
        else:
            return self.__add__(other)
    …

so now, we can use sum to add Day instances!

The full code is:


Django Tip: You can also define the __add__ method in your Django models!

Hope it’s useful!

Google+TwitterLinkedInFacebookReddit

Please, add +Marina Mele in your comments. This way I will get a notification email and I will answer you as soon as possible! :-)