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

Brett Cannon bac at OCF.Berkeley.EDU
Sat Mar 19 16:12:02 CET 2005


[Nick Coghlan]

>> -------------------------
>> 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'
>

That is slightly evil, and here is a simpler example of evilness:

   sum([[1],[2],[3]], [])

Had to look at the source to understand how it was working.  =)  First 
thing learned while at the sprints!

-Brett


More information about the Python-Dev mailing list