[docs] Fwd: There is bug about the built-in function reduce in the document

Shafreeck Sea renenglish at gmail.com
Tue Sep 11 04:42:31 CEST 2012


---------- Forwarded message ----------
From: Shafreeck Sea <renenglish at gmail.com>
Date: 2012/9/6
Subject: There is bug about the built-in function reduce in the document
To: docs at python.org


Hi all:
I found a bug in the document about reduce :
http://docs.python.org/library/functions.html#reduce

Here is the patch:
def reduce(function, iterable, initializer=None):
    it = iter(iterable)
    if initializer is None:
        try:
            initializer = next(it)
        except StopIteration:
            raise TypeError('reduce() of empty sequence with no initial
value')
    accum_value = initializer
-    for x in iterable:
+   for x in it:
        accum_value = function(accum_value, x)
    return accum_value

It duplicated the first element of iterable

For example:
In [4]: reduce(lambda x,y:x+y, [1,2,3,4])
Out[4]: 10

In [5]: docreduce.reduce(lambda x,y:x+y ,[1,2,3,4])
Out[5]: 11

Sorry for my poor english !
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/docs/attachments/20120911/b9f7967b/attachment-0001.html>


More information about the docs mailing list