Re: Feature suggestion: sum() ought to use a compensated summation algorithm
- From: Erik Max Francis <max@xxxxxxxxxxx>
- Date: Sat, 03 May 2008 14:20:37 -0700
Szabolcs Horvát wrote:
Arnaud Delobelle wrote:
sum() works for any sequence of objects with an __add__ method, not
just floats! Your algorithm is specific to floats.
This occurred to me also, but then I tried
sum(['abc', 'efg'], '')
and it did not work. Or is this just a special exception to prevent the misuse of sum to join strings? (As I said, I'm only an occasional user.)
What you wrote is nonsensical there, no different from 'a' + 1 -- which is why it quite rightly raises a TypeError.
You're trying to add a list to a string, which is nonsensical. You add strings to strings, or lists to lists, but mixing them up doesn't make sense. Python can't guess what you mean when you write something like ['abc', 'def'] + '' -- which is the functional equivalent of your call to sum -- and so doesn't try. It indicates this by raising a TypeError.
--
Erik Max Francis && max@xxxxxxxxxxx && http://www.alcyone.com/max/
San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
.
- Follow-Ups:
- Re: Feature suggestion: sum() ought to use a compensated summation algorithm
- From: Torsten Bronger
- Re: Feature suggestion: sum() ought to use a compensated summation algorithm
- References:
- Feature suggestion: sum() ought to use a compensated summation algorithm
- From: Szabolcs Horvát
- Re: Feature suggestion: sum() ought to use a compensated summation algorithm
- From: Arnaud Delobelle
- Re: Feature suggestion: sum() ought to use a compensated summation algorithm
- From: Szabolcs Horvát
- Feature suggestion: sum() ought to use a compensated summation algorithm
- Prev by Date: Re: list.index crashes when the element is not found
- Next by Date: Re: Feature suggestion: sum() ought to use a compensated summation algorithm
- Previous by thread: Re: Feature suggestion: sum() ought to use a compensated summation algorithm
- Next by thread: Re: Feature suggestion: sum() ought to use a compensated summation algorithm
- Index(es):
Relevant Pages
|