[Python-3000] Python 3000 Status Update (Long!)
Nicko van Someren
nicko at nicko.org
Wed Jun 20 19:12:20 CEST 2007
On 20 Jun 2007, at 15:44, Nick Coghlan wrote:
> Nicko van Someren wrote:
>> >>> sum(['a','b','c'])
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> TypeError: unsupported operand type(s) for +: 'int' and 'str'
>> >>> sum([["a"],[u'b'],[3]])
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> TypeError: unsupported operand type(s) for +: 'int' and 'list'
>
> You can already make the second example work properly by supplying
> an appropriate starting value:
>
> >>> sum([["a"],[u'b'],[3]], [])
> ['a', u'b', 3]
>
> (and a similar call will also work for the new bytes type, as well
> as other sequences)
The need to have an explicit 'start' value just seems wrong. It's
horribly inconsistent. Things that can be added to integers work
without initialisers but things that can be added to each other (for
instance numbers in number fields or vectors in vector spaces) can
not. I think in most people's minds the 'sum' operation is like an
evaluation of "+".join(...), you are sticking an addition operation
between the elements of the list. The need to have an explicit
initial value means that sum() is not the sum function for anyone who
does math in any sort of non-standard number space.
> Strings are explicitly disallowed (because Guido doesn't want a
> second way to spell ''.join(seq), as far as I know):
>
> >>> sum(['a','b','c'], '')
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: sum() can't sum strings [use ''.join(seq) instead]
I can appreciate the value of TOOWTDI, and I appreciate that (in the
absence of string concatenation by reference) the performance of
string sum() would suck, but I still think that wilfully making
things inconsistent in order to enforce TOOWTDI is going too far.
Nicko
More information about the Python-3000
mailing list