Skip to content

Commit 11e7d13

Browse files
committed
initial defaults to _functools._initial_missing
* Apply patch by Sergey B Kirpichev <[email protected]> - fix typo * Update docs
1 parent 7b795ba commit 11e7d13

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

Doc/library/functools.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ The :mod:`functools` module defines the following functions:
453453
.. versionadded:: 3.4
454454

455455

456-
.. function:: reduce(function, iterable, /[, initial])
456+
.. function:: reduce(function, iterable, /, initial=_functools._initial_missing)
457457

458458
Apply *function* of two arguments cumulatively to the items of *iterable*, from
459459
left to right, so as to reduce the iterable to a single value. For example,

Modules/_functoolsmodule.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -938,21 +938,22 @@ _functools.reduce
938938
function as func: object
939939
iterable as seq: object
940940
/
941-
initial as result: object(c_default="NULL") = None
941+
initial as result: object(c_default="NULL") = _functools._initial_missing
942942
943-
Apply a function of two arguments cumulatively.
943+
Apply a function of two arguments cumulatively to an iterable, from left to right.
944944
945-
Apply it to the items of a sequence or iterable, from left to right, so as to
946-
reduce the iterable to a single value. For example, reduce(lambda x, y: x+y,
947-
[1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). If initial is present, it is
948-
placed before the items of the iterable in the calculation, and serves as a
949-
default when the iterable is empty.
945+
This efficiently reduces the iterable to a single value. If initial is present,
946+
it is placed before the items of the iterable in the calculation, and serves as
947+
a default when the iterable is empty.
948+
949+
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
950+
calculates ((((1+2)+3)+4)+5).
950951
[clinic start generated code]*/
951952

952953
static PyObject *
953954
_functools_reduce_impl(PyObject *module, PyObject *func, PyObject *seq,
954955
PyObject *result)
955-
/*[clinic end generated code: output=30d898fe1267c79d input=b7082b8b1473fdc2]*/
956+
/*[clinic end generated code: output=30d898fe1267c79d input=40be8069bcbc1a75]*/
956957
{
957958
PyObject *args, *it;
958959

@@ -1794,6 +1795,10 @@ _functools_exec(PyObject *module)
17941795
// lru_list_elem is used only in _lru_cache_wrapper.
17951796
// So we don't expose it in module namespace.
17961797

1798+
if (PyModule_Add(module, "_initial_missing", _PyObject_New(&PyBaseObject_Type)) < 0) {
1799+
return -1;
1800+
}
1801+
17971802
return 0;
17981803
}
17991804

Modules/clinic/_functoolsmodule.c.h

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)