diff options
author | Xuehai Pan <XuehaiPan@pku.edu.cn> | 2023-09-18 16:42:58 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-18 16:42:58 (GMT) |
commit | 74f315edd01b4d6c6c99e50c03a90116820d8d47 (patch) | |
tree | f4346c8e8807e35f6e982b71d1671e370c1ade1b /Doc | |
parent | 0bb0d88e2d4e300946e399e088e2ff60de2ccf8c (diff) | |
download | cpython-74f315edd01b4d6c6c99e50c03a90116820d8d47.zip cpython-74f315edd01b4d6c6c99e50c03a90116820d8d47.tar.gz cpython-74f315edd01b4d6c6c99e50c03a90116820d8d47.tar.bz2 |
gh-102757: fix function signature mismatch for `functools.reduce` between code and documentation (#102759)
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/functools.rst | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/Doc/library/functools.rst b/Doc/library/functools.rst index f736eb0..69ec1eb 100644 --- a/Doc/library/functools.rst +++ b/Doc/library/functools.rst @@ -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 |