`reduce(add,...)` vs `sum(...)`, why the second fails on a list of objects
Question: I have the expectation that `reduce(add,lst)` and `sum(lst)` should give me
the same result, but

    
    
    In [18]: class p():
        def __init__(self, x, y):
            self.x=x ; self.y=y
        def __repr__(self):
            return "(%r,%r)"%(self.x,self.y)
        def __add__(self, P):
            return p(self.x+P.x, self.y+P.y)
       ....:     
    In [19]: pts=[p(1,0), p(2,1), p(-3,4)]
    In [20]: from operator import add
    In [21]: print reduce(add,pts)
    (0,5)
    In [22]: print sum(pts)
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-22-98a81789d257> in <module>()
    ----> 1 print sum(pts)
    TypeError: unsupported operand type(s) for +: 'int' and 'instance'
    In [23]: 
    

of course I'm not understanding something, possibly obvious; could someone
enlighten me?


Answer: `reduce()` starts with `pts[0]` as the initial value (unless you give it an
explicit starting value), but `sum()` defaults with `0`. From the [`sum()`
function documentation](https://docs.python.org/2/library/functions.html#sum):

> `sum(iterable[, start])`  
>  Sums _start_ and the items of an iterable from left to right and returns
> the total. _start_ defaults to `0`.

Give `sum()` a better _start_ value; use the first object just like `reduce()`
does:

    
    
    sum(pts[1:], pts[0])
    

or a suitable empty value:

    
    
    sum(pts, p(0, 0))
    

