# How to overload \_\_add\_\_ method in a self made class to sum multiple objects of the class?

**URL:** <https://discuss.python.org/t/how-to-overload-add-method-in-a-self-made-class-to-sum-multiple-objects-of-the-class/40543>\
**Category:** Python Help\
**Created:** [December 7, 2023, 1:20pm UTC](https://discuss.python.org/t/how-to-overload-add-method-in-a-self-made-class-to-sum-multiple-objects-of-the-class/40543 "2023-12-07T13:20:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dragonsoul](https://avatars.discourse-cdn.com/v4/letter/d/c0e974/32.png) [@dragonsoul](https://discuss.python.org/u/dragonsoul)\
**Post date:** [December 7, 2023, 1:20pm UTC](https://discuss.python.org/t/how-to-overload-add-method-in-a-self-made-class-to-sum-multiple-objects-of-the-class/40543/1 "2023-12-07T13:20:17Z")

</div>

```python
class MyClass:
    def __init__ (self, value):
        self.value = value

    def __add__ (self, other):
        if isinstance(other, MyClass):
            return self.value + other.value
        elif isinstance(other, int) or isinstance(other, float):
            return self.value + other
        else:
            raise TypeError("Unsupported operand type(s) for +")

# Creating objects of MyClass
obj1 = MyClass(10)
obj2 = MyClass(20)
obj3 = MyClass(30)

# Adding multiple objects using the + operator
result = obj1 + obj2 + obj3

print(result) # Output: 60

```

I have received this solution from chat gpt but this does not work. I can’t sum more than 2 objects of my class. How can i solve this?;D (sorry for bad English)

---

<div class="post-metadata">

**Author:** ![ajoino](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ajoino/32/6907_2.png) [@ajoino](https://discuss.python.org/u/ajoino)\
**Post date:** [December 7, 2023, 1:37pm UTC](https://discuss.python.org/t/how-to-overload-add-method-in-a-self-made-class-to-sum-multiple-objects-of-the-class/40543/2 "2023-12-07T13:37:50Z")

</div>

Your ` __add__ ` currently returns an object of whatever type `MyClass.value` is. To be able to chain additions, the ` __add__ ` method should return a `MyClass` instance.

---

<div class="post-metadata">

**Author:** ![ajoino](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ajoino/32/6907_2.png) [@ajoino](https://discuss.python.org/u/ajoino)\
**Post date:** [December 7, 2023, 1:39pm UTC](https://discuss.python.org/t/how-to-overload-add-method-in-a-self-made-class-to-sum-multiple-objects-of-the-class/40543/3 "2023-12-07T13:39:14Z")

</div>

Or you might be able to add an ` __radd__ ` method, but not 100% sure about that.

---

<div class="post-metadata">

**Author:** ![WeisLeDocto](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/weisledocto/32/15827_2.png) [@WeisLeDocto](https://discuss.python.org/u/WeisLeDocto)\
**Post date:** [December 7, 2023, 2:15pm UTC](https://discuss.python.org/t/how-to-overload-add-method-in-a-self-made-class-to-sum-multiple-objects-of-the-class/40543/4 "2023-12-07T14:15:56Z")

</div>

Hi !

When adding two objects in Python, the order of the two objects around the `+` operator matters ! When you call `obj1 + obj2 + obj3`, `obj1 + obj2` is first evaluated and returns an integer (30). Then, `30 + obj3` is evaluated, but the type of 30 (`int`) does not implement addition with objects of type `MyClass`, and you get an error.

If you now call `obj1 + (obj2 + obj3)`, `(obj2 + obj3)` is first evaluated and returns an `int` (50). Then, `obj1 + 50` is evaluated, and since the ` __add__ ` method of `MyClass` supports addition with objects of type `int`, you get the expected result 60 (try it !).

As @ajoino mentioned, the ` __radd__ ` method can solve your problem. Basically, in case the addition first fails (like in `30 + obj3`), it will try the same operation with reversed operands (`obj3 + 30`). The following code works for me:

```auto

class Myclass:

    def __init__ (self, value):
        self.value = value

    def __add__ (self, other):
        if isinstance(other, Myclass):
            return self.value + other.value
        elif isinstance(other, int) or isinstance(other, float):
            return self.value + other
        else:
            raise TypeError("Unsupported operand type(s) for +")

    def __radd__ (self, other):
      return self. __add__ (other)

if __name__ == " __main__":

    obj1 = Myclass(10)
    obj2 = Myclass(20)
    obj3 = Myclass(30)

    result = obj1 + obj2 + obj3

    print(result)

```

---

<div class="post-metadata">

**Author:** ![hansgeunsmeyer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hansgeunsmeyer/32/15188_2.png) [@hansgeunsmeyer](https://discuss.python.org/u/hansgeunsmeyer)\
**Post date:** [December 7, 2023, 2:45pm UTC](https://discuss.python.org/t/how-to-overload-add-method-in-a-self-made-class-to-sum-multiple-objects-of-the-class/40543/5 "2023-12-07T14:45:38Z")

</div>

As Antoine wrote - you should add an ` __radd__ ` method in order to be able to add numerical values to objects of MyClass. But as @ajoino wrote, you should also let your ` __add__ ` method return objects of MyClass; your initial problem was caused by not doing so. It’s formally possible to have the ` __add__ ` method return other types, but it will create a lot of confusion in using the addition operator (as you already noticed yourself).  
So, you could change the method to this (or sth similar):

```auto
def __add__ (self, other):
    if isinstance(other, MyClass):
        return MyClass(self.value + other.value)
    elif isinstance(other, int) or isinstance(other, float):
        return MyClass(self.value + other)
    else:
        raise TypeError("Unsupported operand type(s) for +")

```

If you implement those methods, it’s usually also good to implement ` __iadd__ ` in order to support `+=`.

For a more elaborate example, see: [numbers — Numeric abstract base classes — Python 3.12.0 documentation](https://docs.python.org/3/library/numbers.html#implementing-the-arithmetic-operations).

---

<div class="post-metadata">

**Author:** ![dragonsoul](https://avatars.discourse-cdn.com/v4/letter/d/c0e974/32.png) [@dragonsoul](https://discuss.python.org/u/dragonsoul)\
**Post date:** [December 9, 2023, 7:46pm UTC](https://discuss.python.org/t/how-to-overload-add-method-in-a-self-made-class-to-sum-multiple-objects-of-the-class/40543/6 "2023-12-09T19:46:23Z")

</div>

Wow that’s great. I was just looking for such this answer:D. Thank you so much man.🙂
