Skip to content

Commit

Permalink
gh-102757: fix function signature mismatch for functools.reduce bet…
Browse files Browse the repository at this point in the history
…ween code and documentation (#102759)
  • Loading branch information
XuehaiPan authored Sep 18, 2023
1 parent 0bb0d88 commit 74f315e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
14 changes: 8 additions & 6 deletions Doc/library/functools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -403,25 +403,27 @@ The :mod:`functools` module defines the following functions:
.. versionadded:: 3.4


.. function:: reduce(function, iterable[, initializer])
.. function:: reduce(function, iterable[, initial], /)

Apply *function* of two arguments cumulatively to the items of *iterable*, from
left to right, so as to reduce the iterable to a single value. For example,
``reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])`` calculates ``((((1+2)+3)+4)+5)``.
The left argument, *x*, is the accumulated value and the right argument, *y*, is
the update value from the *iterable*. If the optional *initializer* is present,
the update value from the *iterable*. If the optional *initial* is present,
it is placed before the items of the iterable in the calculation, and serves as
a default when the iterable is empty. If *initializer* is not given and
a default when the iterable is empty. If *initial* is not given and
*iterable* contains only one item, the first item is returned.

Roughly equivalent to::

def reduce(function, iterable, initializer=None):
initial_missing = object()

def reduce(function, iterable, initial=initial_missing, /):
it = iter(iterable)
if initializer is None:
if initial is initial_missing:
value = next(it)
else:
value = initializer
value = initial
for element in it:
value = function(value, element)
return value
Expand Down
2 changes: 1 addition & 1 deletion Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def __ge__(self, other):

def reduce(function, sequence, initial=_initial_missing):
"""
reduce(function, iterable[, initial]) -> value
reduce(function, iterable[, initial], /) -> value
Apply a function of two arguments cumulatively to the items of a sequence
or iterable, from left to right, so as to reduce the iterable to a single
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Align function signature for ``functools.reduce`` in documentation and docstring
with the C implementation.
2 changes: 1 addition & 1 deletion Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ functools_reduce(PyObject *self, PyObject *args)
}

PyDoc_STRVAR(functools_reduce_doc,
"reduce(function, iterable[, initial]) -> value\n\
"reduce(function, iterable[, initial], /) -> value\n\
\n\
Apply a function of two arguments cumulatively to the items of a sequence\n\
or iterable, from left to right, so as to reduce the iterable to a single\n\
Expand Down

0 comments on commit 74f315e

Please sign in to comment.