restriction on sum: intentional bug?
aahz at pythoncraft.com
Sat Oct 17 16:27:44 CEST 2009
In article <0062f568$0$26941$c3e8da3 at news.astraweb.com>,
Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> wrote:
>Mathematically, sum() is defined as the repeated application of the +
>operator. In Python, the + operator is well-defined for strings and lists
>as well as numbers. Since you can say "ab" + "cd" + "ef" and get a
>sensible result, then sum() should be able to do the same thing.
>And indeed, if you pass a list-of-lists to sum(), it does:
>>>> sum([[1,2], ['a',None], [1,'b']], )
>[1, 2, 'a', None, 1, 'b']
>(For the record, summing lists is O(N**2), and unlike strings, there's no
>optimization in CPython to avoid the slow behaviour.)
Are you sure?
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
"To me vi is Zen. To use vi is to practice zen. Every command is a
koan. Profound to the user, unintelligible to the uninitiated. You
discover truth everytime you use it." --reddy at lion.austin.ibm.com
More information about the Python-list