[Python-Dev] Fwd: summing a bunch of numbers (or "whatevers")
Mon, 21 Apr 2003 08:12:25 -0400 (EDT)
On Mon, 21 Apr 2003, Alex Martelli wrote:
> On Monday 21 April 2003 10:52 am, Alex Martelli wrote:
> > Aye aye, cap'n -- now that youve crossed the i's and dotted the t's
> > I'll arrange the complete patch with tests and docs and submit it
> > forthwith.
> Done -- patch 724936 on SF, assigned to gvanrossum with priority 7
> as you said to do for patches meant for 2.3beta1.
Just to make sure I understand the desired semantics, is this Python
implementation of sum() accurate:
'''sum(sequence) -> value
Returns the sum of a non-empty sequence of numbers (or other objects
that can be added to each other, such as strings, lists, tuples...).'''
it = iter(l)
next = it.next
first = next()
raise ValueError, 'sum() arg is an empty sequence'
# Special-case sequences of strings, for speed
if isinstance(first, str):
return first + ''.join(it)
first += next()
The speed optimization for string sequences is slightly different, but
exposes the same fast-path for the vast majority of likely inputs.
The OPAL Group - Enterprise Systems Architect
Voice: (216) 986-0710 x 19 E-mail: firstname.lastname@example.org
Fax: (216) 986-0714 WWW: http://www.theopalgroup.com