This commit is contained in:
Raymond Hettinger 2013-10-12 16:04:39 -07:00
commit 84fc7081f5
2 changed files with 15 additions and 0 deletions

View file

@ -205,6 +205,18 @@ The :mod:`functools` module defines the following functions:
a default when the sequence is empty. If *initializer* is not given and
*sequence* contains only one item, the first item is returned.
Equivalent to::
def reduce(function, iterable, initializer=None):
it = iter(iterable)
if initializer is None:
value = next(it)
else:
value = initializer
for element in it:
value = function(value, element)
return value
.. decorator:: singledispatch(default)