@@ -932,15 +932,31 @@ _functools_cmp_to_key_impl(PyObject *module, PyObject *mycmp)
932
932
933
933
/* reduce (used to be a builtin) ********************************************/
934
934
935
- // Not converted to argument clinic, because of `args` in-place modification.
936
- // AC will affect performance.
935
+ /*[clinic input]
936
+ _functools.reduce
937
+
938
+ function as func: object
939
+ iterable as seq: object
940
+ initial as result: object = NULL
941
+ /
942
+
943
+ Apply a function of two arguments cumulatively to the items of an iterable, from left to right.
944
+
945
+ This effectively 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).
951
+ [clinic start generated code]*/
952
+
937
953
static PyObject *
938
- functools_reduce (PyObject * self , PyObject * args )
954
+ _functools_reduce_impl (PyObject * module , PyObject * func , PyObject * seq ,
955
+ PyObject * result )
956
+ /*[clinic end generated code: output=30d898fe1267c79d input=d233c2670cba7f66]*/
939
957
{
940
- PyObject * seq , * func , * result = NULL , * it ;
958
+ PyObject * args , * it ;
941
959
942
- if (!PyArg_UnpackTuple (args , "reduce" , 2 , 3 , & func , & seq , & result ))
943
- return NULL ;
944
960
if (result != NULL )
945
961
Py_INCREF (result );
946
962
@@ -1006,18 +1022,6 @@ functools_reduce(PyObject *self, PyObject *args)
1006
1022
return NULL ;
1007
1023
}
1008
1024
1009
- PyDoc_STRVAR (functools_reduce_doc ,
1010
- "reduce(function, iterable[, initial], /) -> value\n\
1011
- \n\
1012
- Apply a function of two arguments cumulatively to the items of an iterable, from left to right.\n\
1013
- \n\
1014
- This effectively reduces the iterable to a single value. If initial is present,\n\
1015
- it is placed before the items of the iterable in the calculation, and serves as\n\
1016
- a default when the iterable is empty.\n\
1017
- \n\
1018
- For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])\n\
1019
- calculates ((((1 + 2) + 3) + 4) + 5)." );
1020
-
1021
1025
/* lru_cache object **********************************************************/
1022
1026
1023
1027
/* There are four principal algorithmic differences from the pure python version:
@@ -1722,7 +1726,7 @@ PyDoc_STRVAR(_functools_doc,
1722
1726
"Tools that operate on functions." );
1723
1727
1724
1728
static PyMethodDef _functools_methods [] = {
1725
- { "reduce" , functools_reduce , METH_VARARGS , functools_reduce_doc },
1729
+ _FUNCTOOLS_REDUCE_METHODDEF
1726
1730
_FUNCTOOLS_CMP_TO_KEY_METHODDEF
1727
1731
{NULL , NULL } /* sentinel */
1728
1732
};
0 commit comments