From da36f675428c4980564aaff02889d5e12d932e97 Mon Sep 17 00:00:00 2001 From: Alexander Eder Date: Sat, 26 Jul 2025 23:36:07 +0200 Subject: [PATCH] Remove ambiguous part in docstring of functools.reduce() Old description could be read that "initial" will only be used if "iterable" is empty. This is not the case. Plus include additional sentence from docs.python.org --- Lib/functools.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/functools.py b/Lib/functools.py index 7f0eac3f650209..7b592caf182ef2 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -241,8 +241,9 @@ def reduce(function, sequence, initial=_initial_missing): 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 - a default when the iterable is empty. + it is placed before the items of the iterable in the calculation. If the iterable is empty and + initial is given, initial will be returned. If initial is not given and iterable contains only + one item, the first item is returned. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1 + 2) + 3) + 4) + 5).