max(), sum(), next()

Mel mwilson at the-wire.com
Sat Sep 6 17:03:10 CEST 2008


Steven D'Aprano wrote:

> On Sat, 06 Sep 2008 00:33:25 -0400, Manu Hack wrote:
> 
>> Actually it's even more natural to state sum([x]) = x, and this way you
>> can never conclude that sum([]) = 0 from there.
> 
> But what you can say is that for any list L, sum(L) = sum(L + [0]).
> 
> Therefore sum([]) = sum([] +[0]) = 0

Yep.  The way it is preserves the distributive property

sum(a+b) = sum(a) + sum(b)

This would matter in cases like (untested code..)

    suvsales = sum (sum (s.price for s in d.sales if s.class='suv') for d in
districts)


        Mel.



More information about the Python-list mailing list