[Python-ideas] reduce(func, seq, initial=0)

Chris Angelico rosuav at gmail.com
Fri Oct 10 12:07:08 CEST 2014


On Fri, Oct 10, 2014 at 8:11 PM, Stephan Sahm <Stephan.Sahm at gmx.de> wrote:
> I stumbled upon the builtin reduce function. It has an optional parameter
> initial, however if trying to set it like
>
> import operator as op
> reduce(op.add, range(10), initial=0)
>
> I get
>
> TypeError: reduce() takes no keyword arguments
>
>
> I think it would be really straightforward to also add keyword
> possibilities, at least to the parameter initial.

The first thing to note is that reduce(), in Python 3, is now imported
from functools; if you're working with Python 2, you won't see this
changed unless you can convince people that this is a bug to be fixed.
However, prefixing your code with "from functools import reduce"
produces the exact same result in Python 3.

The question is, though: What's the advantage? The first two arguments
are mandatory, and there's only one optional argument. Do you need to
say "initial=" on that?

ChrisA


More information about the Python-ideas mailing list