From a22cef96baa8734eb5879cf532fb9e1df253c704 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sun, 27 Oct 2024 18:24:52 +0300 Subject: [PATCH 1/4] Improve functools.reduce() docstring PEP 257 says that "Multi-line docstrings consist of a summary line just like a one-line docstring, followed by a blank line, followed by a more elaborate description." This is also a requirement from the Argument Clinic. --- Lib/functools.py | 14 ++++++++------ Modules/_functoolsmodule.c | 14 ++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Lib/functools.py b/Lib/functools.py index 9d53d3601559b2..1917a3b688cedd 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -238,12 +238,14 @@ def reduce(function, sequence, initial=_initial_missing): """ 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 - value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates - ((((1+2)+3)+4)+5). If 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. + Apply a function of two arguments cumulatively to an iterable, from left to right. + + This efficiently reduces the iterable to a single value. If 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. + + For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) + calculates ((((1 + 2) + 3) + 4) + 5). """ it = iter(sequence) diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c index 802b1cf792c555..a57246be99151e 100644 --- a/Modules/_functoolsmodule.c +++ b/Modules/_functoolsmodule.c @@ -1009,12 +1009,14 @@ functools_reduce(PyObject *self, PyObject *args) PyDoc_STRVAR(functools_reduce_doc, "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\ -value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\ -((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\ -of the iterable in the calculation, and serves as a default when the\n\ -iterable is empty."); +Apply a function of two arguments cumulatively to an iterable, from left to right.\n\ +\n\ +This efficiently reduces the iterable to a single value. If initial is present,\n\ +it is placed before the items of the iterable in the calculation, and serves as\n\ +a default when the iterable is empty.\n\ +\n\ +For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])\n\ +calculates ((((1 + 2) + 3) + 4) + 5)."); /* lru_cache object **********************************************************/ From ea5ae96662f8cb32b996c96bed5c0ba8f67726af Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 28 Oct 2024 07:26:15 +0300 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Erlend E. Aasland --- Lib/functools.py | 2 +- Modules/_functoolsmodule.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/functools.py b/Lib/functools.py index 1917a3b688cedd..b64e931c6bdd20 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -240,7 +240,7 @@ def reduce(function, sequence, initial=_initial_missing): Apply a function of two arguments cumulatively to an iterable, from left to right. - This efficiently reduces the iterable to a single value. If initial is present, + This effectively reduces the iterable to a single value. If 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. diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c index a57246be99151e..da87f7304553c9 100644 --- a/Modules/_functoolsmodule.c +++ b/Modules/_functoolsmodule.c @@ -1011,7 +1011,7 @@ PyDoc_STRVAR(functools_reduce_doc, \n\ Apply a function of two arguments cumulatively to an iterable, from left to right.\n\ \n\ -This efficiently reduces the iterable to a single value. If initial is present,\n\ +This effectively reduces the iterable to a single value. If initial is present,\n\ it is placed before the items of the iterable in the calculation, and serves as\n\ a default when the iterable is empty.\n\ \n\ From fed491178a9917b73242560a6a2e13eb3068b283 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Tue, 29 Oct 2024 04:33:45 +0300 Subject: [PATCH 3/4] Update Lib/functools.py Co-authored-by: Erlend E. Aasland --- Lib/functools.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/functools.py b/Lib/functools.py index b64e931c6bdd20..27abd622a8cff1 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -238,7 +238,7 @@ def reduce(function, sequence, initial=_initial_missing): """ reduce(function, iterable[, initial], /) -> value - Apply a function of two arguments cumulatively to an iterable, from left to right. + Apply a function of two arguments cumulatively to the items of an iterable, from left to right. This effectively reduces the iterable to a single value. If initial is present, it is placed before the items of the iterable in the calculation, and serves as From e0e467c84a65313bc18a4b10c70f5c1277f3e903 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 29 Oct 2024 08:42:54 +0100 Subject: [PATCH 4/4] Update Modules/_functoolsmodule.c --- Modules/_functoolsmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c index da87f7304553c9..da4e088e54621e 100644 --- a/Modules/_functoolsmodule.c +++ b/Modules/_functoolsmodule.c @@ -1009,7 +1009,7 @@ functools_reduce(PyObject *self, PyObject *args) PyDoc_STRVAR(functools_reduce_doc, "reduce(function, iterable[, initial], /) -> value\n\ \n\ -Apply a function of two arguments cumulatively to an iterable, from left to right.\n\ +Apply a function of two arguments cumulatively to the items of an iterable, from left to right.\n\ \n\ This effectively reduces the iterable to a single value. If initial is present,\n\ it is placed before the items of the iterable in the calculation, and serves as\n\