This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author terry.reedy
Recipients docs@python, terry.reedy
Date 2014-12-14.00:27:25
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1418516847.13.0.26860108886.issue23049@psf.upfronthosting.co.za>
In-reply-to
Content
from functools import reduce
def add(a,b): return a+b
reduce(add, {})
>>>
Traceback (most recent call last):
  File "C:\Programs\Python34\tem.py", line 3, in <module>
    reduce(add, {})
TypeError: reduce() of empty sequence with no initial value

However, the reduce-equivalent code in the doc sets a bad example and forgets to account for empty iterators.

def reduce(function, iterable, initializer=None):
    it = iter(iterable)
    if initializer is None:
        value = next(it)
    else: ...

So it lets the StopIteration escape (a bad practice that can silently break iterators).  The code should be

def reduce(function, iterable, initializer=None):
    it = iter(iterable)
    if initializer is None:
        try:
            value = next(it)
        except StopIteration:
            raise TypeError("reduce() of empty sequence with no initial value") from None
    else: ...

(patch coming)
History
Date User Action Args
2014-12-14 00:27:27terry.reedysetrecipients: + terry.reedy, docs@python
2014-12-14 00:27:27terry.reedysetmessageid: <1418516847.13.0.26860108886.issue23049@psf.upfronthosting.co.za>
2014-12-14 00:27:27terry.reedylinkissue23049 messages
2014-12-14 00:27:25terry.reedycreate