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

Nick Coghlan ncoghlan at iinet.net.au
Fri Mar 18 11:57:59 CET 2005


> -------------------------
> sum() semantics discussed
> -------------------------
> Guido's blog entry on `the fate of reduce() in Python 3000`_ (which 
> reiterated Guido's plan to cut map(), reduce(), filter() and lambdas 
> (what about zip()?) caused a huge discussion on whether sum() worked the 
> best way possible.  As it stands now, sum() only accepts a sequence of 
> numbers and its optional second argument works as an initial value to 
> build off of.

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
...
Py> sum(["a"] * 5, additive_identity())
'aaaaa'

This is fairly abusive of sum, though :)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net


More information about the Python-Dev mailing list