15

Is there something like a __sum__ method, similar to __add__, in order to sum up a list of instances of classes into a new class instance?

I need this because in my case sum([a,b,c]) should be different from sum([sum([a,b]), c]). In other words, the sum really depends on an arbitrary number of arguments, and cannot be defined in terms of a binary operation __add__.

5
  • can you please add a class definition to the post?
    – aydow
    Jun 26, 2018 at 6:31
  • 5
    If your operation is not associative, maybe it should not be called + / sum.
    – melpomene
    Jun 26, 2018 at 6:34
  • 5
    No, you can't hook into what sum does. It just iterates over the container and invokes __add__, it doesn't have separate behaviour.
    – jonrsharpe
    Jun 26, 2018 at 6:49
  • Thank you @jonrsharpe, this seems to be the answer to my question.
    – Giove
    Jun 26, 2018 at 6:56
  • Remarkably similar to this question which asks What method, if any, is used when calling sum()? But if your operation is not associative then why are you trying to use the built-in function sum for it?
    – kaya3
    Mar 3, 2020 at 18:24

2 Answers 2

30

Summing will not work if you just define __add__ because as this explains, the sum function starts with the integer 0, and 0 + YourClass will be undefined.

So, you need to define __radd__ as well. Here's an implementation that will allow you to sum:

def __radd__(self, other):
    if other == 0:
        return self
    else:
        return self.__add__(other)

Now your class should be able to sum just fine.

EDIT: (I'm aware that this does not solve OP's problem, but it solves the problem that people come to this question to solve, so I'm leaving it.)

2
  • 3
    Or just call it like sum(..., start) to provide an appropriate identity element.
    – kaya3
    Mar 3, 2020 at 18:23
  • I've seen this comment multiple times over the years, and never understood it, but just understood it today. If I'm right, you're saying that if you're summing up a list like sum([obj1, obj2, obj3]), and it's not working, you could add a neutral object to the start of the list where addition is defined, like sum([Obj(0, 0, 0), obj1, obj2, obj3]).
    – Pro Q
    Nov 26, 2021 at 22:32
-2

sum is a built-in function in Python. __add__ is a special method. Every container object such as list has an __add__ method. You can see it in the guide. The __add__ methods in container objects and numeric objects work differently.

l = [1, 2, 3]
b = [4, 5, 6]

# Concatenation using .__add__()
print(l.__add__(b))

The result will be the concatenation of two lists,

[1, 2, 3, 4, 5, 6]

while the __add__ method in numeric objects will do the addition operation.

x = 2
y = 3

# addition operation: 2 + 3 = 5
print(x.__add__(y))

You can create a custom list with a sum method.

class Custom(list):
    # adding sum() functionallity
    def sumup(self):
        return sum(self)

Create new custom list object like this,

l = Custom([1, 2, 3])

and call the sumup method to sum all values inside the custom list

l.sumup()
2
  • 3
    read the question again. I think you misunderstood what OP wants
    – hjpotter92
    Jun 26, 2018 at 6:56
  • 3
    "the add method will invoked whenever '+' signs is presented", no: __radd__
    – Arount
    Jun 26, 2018 at 6:57

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.