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, like
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
File: marinamele_add_method.py ------------------------------ class Day(object): def __init__(self, visits, contacts): self.visits = visits self.contacts = contacts def __add__(self, other): total_visits = self.visits + other.visits total_contacts = self.contacts + other.contacts return Day(total_visits, total_contacts) def __radd__(self, other): if other == 0: return self else: return self.__add__(other) def __str__(self): return "Visits: %i, Contacts: %i" % (self.visits, self.contacts) day1 = Day(10, 1) day2 = Day(20, 2) print day1 # Visits: 10, Contacts: 1 print day2 # Visits: 20, Contacts: 2 day3 = day1 + day2 print day3 # Visits: 30, Contacts: 3 day4 = sum([day1, day2, day3]) print day4 # Visits: 60, Contacts: 6 |
Django Tip: You can also define the __add__ method in your Django models!
Hope it’s useful!
New Post! Modify the __add__ method of a Python Class to control the sum of two custom objects http://t.co/3IZIn9wG2z
— Marina Mele (@Marina_Mele) April 16, 2014
Please, add +Marina Mele in your comments. This way I will get a notification email and I will answer you as soon as possible! :-)