-------------------------
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   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---------------------------------------------------------------
            http://boredomandlaziness.skystorm.net
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to