I have a class defined as
class MyClass(object):
def __init__(self, value=0):
self.value = value
def __add__(self,other):
return MyClass(self.value + other.value)
__radd__ = __add__
I would like simply to apply the sum
function to them like this:
a=MyClass(value=1)
b=MyClass(value=2)
c=[a,b]
print sum(c) # should return a MyClass instance with value 3
as was suggested in this post. However, an exception is raised:
15
16 c=[a,b]
---> 17 print sum(c)
TypeError: unsupported operand type(s) for +: 'int' and 'MyClass'
I don't understand why the sum
function wants to add two different types.