max(), sum(), next()
Tino Wildenhain
tino at wildenhain.de
Thu Sep 11 04:51:11 EDT 2008
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3241 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20080911/fbe7b3ce/attachment.bin>
More information about the Python-list
mailing list