Overriding "+=" in Python? (__iadd__() method)

Evan Fosmark picture Evan Fosmark · Jun 26, 2009 · Viewed 45.6k times · Source

Is it possible to override += in Python?

Answer

John Kugelman picture John Kugelman · Jun 26, 2009

Yes, override the __iadd__ method. Example:

def __iadd__(self, other):
    self.number += other.number
    return self