sum for sequences?

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Sun Mar 28 21:34:54 EDT 2010


On Sun, 28 Mar 2010 13:49:32 -0700, Paul Rubin wrote:

> Steve Howell <showell30 at yahoo.com> writes:
>> The documentation is pretty clear on the intention that sum() is
>> intended for numbers: ...
> 
> I've never been big on the idea of duck-typing addition.  Who would have
> thought that (1,2,3)+(4.5.6) was something other than the vector sum?

But your arguments are tuples, not vectors.

There are languages which do treat arithmetic operations on vectors as 
vector operations, as do (e.g.) H-P scientific calculators. That's a fine 
design choice, and it works for languages where the emphasis is on 
scientific calculations. But Python is a more generalist language, so in 
my mind it is more appropriate to treat lists as generic lists, and not 
numeric vectors:

[1, 2, 3] + [4, "a"] => [1, 2, 3, 4, "a"]

just as

"123" + "4a" => "1234a"



-- 
Steven



More information about the Python-list mailing list