]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-102757: fix function signature mismatch for `functools.reduce` between code and...
authorXuehai Pan <XuehaiPan@pku.edu.cn>
Mon, 18 Sep 2023 16:42:58 +0000 (00:42 +0800)
committerGitHub <noreply@github.com>
Mon, 18 Sep 2023 16:42:58 +0000 (10:42 -0600)
Doc/library/functools.rst
Lib/functools.py
Misc/NEWS.d/next/Documentation/2023-03-16-15-39-26.gh-issue-102759.ehpHw6.rst [new file with mode: 0644]
Modules/_functoolsmodule.c

index f736eb0aca1ac5d1436397047b2cd0c79145aa2c..69ec1eb3ecd89d4e575810ca5e4b980b6dabdc88 100644 (file)
@@ -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
index a2fc28779dbddcd238ced775cbe0afe044a0f375..6cb532323b1d67cb1cdd31bc5ad43536436316e7 100644 (file)
@@ -236,7 +236,7 @@ _initial_missing = object()
 
 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
diff --git a/Misc/NEWS.d/next/Documentation/2023-03-16-15-39-26.gh-issue-102759.ehpHw6.rst b/Misc/NEWS.d/next/Documentation/2023-03-16-15-39-26.gh-issue-102759.ehpHw6.rst
new file mode 100644 (file)
index 0000000..d3df6c8
--- /dev/null
@@ -0,0 +1,2 @@
+Align function signature for ``functools.reduce`` in documentation and docstring
+with the C implementation.
index 389ff4391de0be69adeb9494a438d1be1400d862..8ea493ad9ab278e1b67f73f6271f4ddf60a4059d 100644 (file)
@@ -725,7 +725,7 @@ Fail:
 }
 
 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\