Skip to content

Commit 74f315e

Browse files
authored
gh-102757: fix function signature mismatch for functools.reduce between code and documentation (#102759)
1 parent 0bb0d88 commit 74f315e

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

Doc/library/functools.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -403,25 +403,27 @@ The :mod:`functools` module defines the following functions:
403403
.. versionadded:: 3.4
404404

405405

406-
.. function:: reduce(function, iterable[, initializer])
406+
.. function:: reduce(function, iterable[, initial], /)
407407

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

417417
Roughly equivalent to::
418418

419-
def reduce(function, iterable, initializer=None):
419+
initial_missing = object()
420+
421+
def reduce(function, iterable, initial=initial_missing, /):
420422
it = iter(iterable)
421-
if initializer is None:
423+
if initial is initial_missing:
422424
value = next(it)
423425
else:
424-
value = initializer
426+
value = initial
425427
for element in it:
426428
value = function(value, element)
427429
return value

Lib/functools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def __ge__(self, other):
236236

237237
def reduce(function, sequence, initial=_initial_missing):
238238
"""
239-
reduce(function, iterable[, initial]) -> value
239+
reduce(function, iterable[, initial], /) -> value
240240
241241
Apply a function of two arguments cumulatively to the items of a sequence
242242
or iterable, from left to right, so as to reduce the iterable to a single
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Align function signature for ``functools.reduce`` in documentation and docstring
2+
with the C implementation.

Modules/_functoolsmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ functools_reduce(PyObject *self, PyObject *args)
725725
}
726726

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

0 commit comments

Comments
 (0)