max(), sum(), next()

Boris Borcic bborcic at gmail.com
Sat Sep 13 12:00:42 CEST 2008


Tino Wildenhain wrote:
> Hi,
> 
> Luis Zarrabeitia wrote:
>> Quoting Laszlo Nagy <gandalf at shopzeus.com>:
>>
> ...
>> Even better:
>>
>> help(sum) shows
>>
>> ===
>> sum(...)
>>     sum(sequence, start=0) -> value
>>         Returns the sum of a sequence of numbers (NOT strings) plus 
>> the value
>>     of parameter 'start'.  When the sequence is empty, returns start.
>> ===
>>
>> so the fact that sum([]) returns zero is just because the start value 
>> is zero...
>> sum([],object()) would return an object().
>>
>> BTW, the original code:
>>
>>>>> sum(s for s in ["a", "b"] if len(s) > 2)
>>
>> wouldn't work anyway... it seems that sum doesn't like to sum strings:
>>
>>>>> sum(['a','b'],'')
>>
>> <type 'exceptions.TypeError'>: sum() can't sum strings [use 
>> ''.join(seq) instead]
> 
> Yes which is a bit bad anyway. I don't think hard wiring it is such a 
> nice idea. You know, walks like a duck, smells like a duck...
> If it makes sense to handle things differently for performance, then
> please have it doing it silently, e.g. when it detects strings just
> use join() internally.
> 
> Cheers
> Tino

+1

''.join is horrible. And it adds insult to injury that S.join(S.split(T)) != T 
as a rule. The interpreter has no business to patronize us into this shamefully 
contorted neighborhood while it understands what we want.

Cheers, BB




More information about the Python-list mailing list