sum and strings

Neil Cerutti horpner at yahoo.com
Fri Aug 25 11:08:31 EDT 2006


On 2006-08-25, Paul Rubin <http> wrote:
> Bryan Olson <fakeaddress at nowhere.org> writes:
>> And herein is the problem: A class may implement "__add__" any
>> way the programmer chooses. Python should require, or at least
>> document requirements, on further properties of addition.
>> Python should insist that addition be symmetric an transitive,
>> and classes implementing addition provide an additive
>> identity.
>
> Are you saying "abc"+"def" should not be concatenation?  I
> guess that's reasonable.  As long as + is string concatenation
> though, the principle of least astonishment suggests that "sum"
> should conconcatenate several strings.
>
> I'm not sure what you mean by addition being symmetric or
> transitive; it is not an equivalence relation.  Do you mean
> commutative and associative?  I'm not sure if floating-point
> arithmetic has those properties, strictly speaking.

The interesting part of the discussion, to me, was that Alex
Martelli's initial implementation included the string
optimization so that a list with a string as its first element
called ''.join(lst) instead. But that optimization turns out to
be valid only if every element of the list is a string.

So there isn't, it seems, a practical way of implementing the
sum(list of strings) -> ''.join(list of strings optimization.

-- 
Neil Cerutti
8 new choir robes are currently needed, due to the addition of
several new members and to the deterioration of some of the
older ones. --Church Bulletin Blooper 



More information about the Python-list mailing list