Re: Feature suggestion: sum() ought to use a compensated summation algorithm



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
.



Relevant Pages

  • Re: id functions of ints, floats and strings
    ... My observations of its behaviour when comparing ints, floats and ... I didn't mention any examples with strings; they behaved like ints ... Does the behaviour of ids for lists and tuples of the same element ...
    (comp.lang.python)
  • id functions of ints, floats and strings
    ... My observations of its behaviour when comparing ints, floats and ... I didn't mention any examples with strings; they behaved like ints ... Does the behaviour of ids for lists and tuples of the same element ...
    (comp.lang.python)
  • Re: Average of entries in a list of lists
    ... I have a list of lists, with around 1000 lists looking like this: ... If you can guarantee that your numbers will already be floats, ... Now you need another function which extracts all the numbers from your ... if you have a list of numbers in the form of strings: ...
    (comp.lang.python)
  • Re: Referencing Items in a List of Tuples
    ... five Python books or a Google search tell me how to refer to specific items ... two items are 3-character strings, the remaining 28 itmes are floats. ... I would really appreciate a pointer so I can learn how to manipulate lists ...
    (comp.lang.python)
  • Re: question about True values
    ... numpy arrays which deliberately raise an exception because they don't make ... lists was actually useful. ... and floats? ... sequences, mappings, numeric types, the same syntax works. ...
    (comp.lang.python)