[Patches] [ python-Patches-724936 ] new sum builtin

SourceForge.net noreply@sourceforge.net
Tue, 22 Apr 2003 02:27:47 -0700


Patches item #724936, was opened at 2003-04-21 12:33
Message generated for change (Comment added) made by aleax
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=724936&group_id=5470

Category: Core (C code)
Group: Python 2.3
>Status: Closed
>Resolution: Accepted
Priority: 7
Submitted By: Alex Martelli (aleax)
Assigned to: Alex Martelli (aleax)
Summary: new sum builtin

Initial Comment:
as per discussion on Python-Dev.  Note that 
sum(manystrings), while still FAR faster than 
reduce(operator.add, manystrings) and the equivalent loop, 
is now about twice as slow as ''.join(manystrings) -- to 
support  the possibility that "manystrings" may be an 
iterator, sum must retain the first item and 
string-concatenate to it the ''.join of the others.  I have not 
mentioned this slight performance hit in the docs (just as 
the far more severe performance trap with 
reduce(operator.add, manystrings) is nowhere mentioned).  
Also, I have not edited the tutorial.  Please let me know if 
you want more editing of such docs, I'll be quite glad to do 
it, of course!


----------------------------------------------------------------------

>Comment By: Alex Martelli (aleax)
Date: 2003-04-22 11:27

Message:
Logged In: YES 
user_id=60314

checked it in (including further fixes suggested by rhettinger -- tx!).

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2003-04-21 21:19

Message:
Logged In: YES 
user_id=6380

OK, looks good.  You can check it in yourself -- I've given
you commit priviliges!

----------------------------------------------------------------------

Comment By: Alex Martelli (aleax)
Date: 2003-04-21 21:17

Message:
Logged In: YES 
user_id=60314

oops, had forgotten to handle the case of PyInt_FromLong(0) 
failing -- fixed that.

----------------------------------------------------------------------

Comment By: Alex Martelli (aleax)
Date: 2003-04-21 21:12

Message:
Logged In: YES 
user_id=60314

ok, now it only forbids strings, and allows other sequences.  Also 
added a short paragraph to the tutorial as you indicated.


----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2003-04-21 20:27

Message:
Logged In: YES 
user_id=6380

Hm...  What if someone defines a Vector class which defines
__add__ as elementwise addition, but also supports sequence
behavior to access the vector items? You couldn't use a
Vector as the start value, so you couldn't sum Vectors.

I would either specifically disallow list and tuple, or not
bother beyond disallowing strings (which probably captures
95% of the mistates anyway).

Also, "result = PyInt_FromLong(0);" is missing error
checking (though it is unlikely to fail).

Since the concept of higher order functions is useful in the
tutorial, perhaps  all you need to add is a statement at the
end of the section on reduce() saying something like "don't
use this example; because summing numbers is so common, a
built-in function sum() that works like this is provided".
With a \versionadded{2.3} marker.

----------------------------------------------------------------------

Comment By: Alex Martelli (aleax)
Date: 2003-04-21 20:13

Message:
Logged In: YES 
user_id=60314

OK, changed again as requested.  I have forbidden all 
sequences (not just strings) as the value of start, since, as you 
mention, performance problems would arise anyway.  Yes, the 
tutorial does cover reduce (specific example is reduce(add,...)) 
together with the other builtin higher-order functions.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2003-04-21 17:52

Message:
Logged In: YES 
user_id=6380

I thought we would allow a second argument specifying the
starting point, defaulting to 0 (so w is never NULL).  See
my pseudo code posted to python-dev. (Except if it used +=,
ignore that.)

Also, I would refrain from mentioning lists or tuples in the
docs, since those in fact have the same O(N**2) behavior as
strings. better use datetime.timedelta as the example.

What would you suggest to modify in the tutorial?  Does it
even mention reduce()?

----------------------------------------------------------------------

Comment By: Alex Martelli (aleax)
Date: 2003-04-21 17:44

Message:
Logged In: YES 
user_id=60314

modified as per GvR's pronouncement on python-dev: now 
sum([]) returns 0, and it's specifically forbidden for the first item of 
the argument to be a string (a TypeError is raised and the 
message tells the user to use ''.join instead).  Guido, please do let 
me know if I should edit the tutorial as well, thanks!


----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2003-04-21 14:21

Message:
Logged In: YES 
user_id=6380

I have to rethink the idea that sum() should only apply to
numbers. The discussion brought up some new good points that
I need to ponder.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=724936&group_id=5470