[Python-Dev] python-dev Summary for 2005-03-01 through 2005-03-15 [draft]

Kurt B. Kaiser kbk at shore.net
Sat Mar 19 18:49:20 CET 2005


Nick Coghlan <ncoghlan at iinet.net.au> writes:

> That last sentence isn't quite true. With an appropriate second
> argument, sum can be used to sum any sequence (even one containing
> strings):
>
> Py> class additive_identity(object):
> ...   def __add__(self, other):
> ...     return other
> ...

==> setup

> Py> sum(["a"] * 5, additive_identity())
> 'aaaaa'
>
> This is fairly abusive of sum, though :)

Python 2.5a0 (#85, Jan 27 2005, 17:41:04) 
[GCC 2.95.3 20010125 (prerelease, propolice)] on openbsd3
Type "copyright", "credits" or "license()" for more information.

IDLE 1.2a0

Py> t = timeit.Timer("sum(('a','bcd', 'e'), ai())", setup)
Py> t.repeat()
[3.3861918449401855, 3.2027261257171631, 3.1891348361968994]

Py> t2 = timeit.Timer("''.join(('a','bcd', 'e'))")
Py> t2.repeat()
[1.1249339580535889, 1.1143810749053955, 1.0990779399871826]

Py> t3 = timeit.Timer("'a' + 'bcd' + 'e'")
Py> t3.repeat()
[0.10233211517333984, 0.099857091903686523, 0.10514688491821289]

-- 
KBK


More information about the Python-Dev mailing list