Is there a simple and quick way to use sum() with non-integer values?
So I can use it like this:
class Foo(object):
def __init__(self,bar)
self.bar=bar
mylist=[Foo(3),Foo(34),Foo(63),200]
result=sum(mylist) # result should be 300
I tried overriding __add__
and __int__
etc, but I don't have found a solution yet
EDIT:
The solution is to implement:
def __radd__(self, other):
return other + self.bar
as Will suggested in his post. But as always, all roads lead to Rome, but I think this is the best solution since I don't need __add__
in my class
Try:
sum() works probably faster, but it is specialized for builtin numbers only. Of course you still have to provide a method to add your Foo() objects. So full example:
You may also need to implement the
__radd__
function, which represents "reverse add" and is called when the arguments can't be resolved in the "forward" direction. For example,x + y
is evaluated asx.__add__(y)
if possible, but if that doesn't exist then Python triesy.__radd__(x)
.Since the
sum()
function starts with the integer0
, the first thing it does is try to evaluate:which will require that you implement
Foo.__radd__
.Or if you don't want to import anything,
Another small advantage is that you don't have to necessarily declare an
__add__
method as part of your Foo objects, if this happens to be the only circumstance in which you'd want to do addition. (But it probably wouldn't hurt to define__add__
for future flexibility.)Try using the
__int__
method and then mapping each element in your list to theint
function to get the values out:Its a bit tricky - the sum() function takes the start and adds it to the next and so on
You need to implement the
__radd__
method: