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

Steven D'Aprano steve at pearwood.info
Fri Oct 10 12:10:10 CEST 2014


On Fri, Oct 10, 2014 at 11:11:36AM +0200, Stephan Sahm wrote:
> Hi folk,
> 
> 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.

Unfortunately, this is a limitation of many built-in functions and 
methods, possibly the majority. E.g.:


Help on built-in function getattr in module builtins:
getattr(...)
    getattr(object, name[, default]) -> value


but:

py> getattr(None, name='spam', default=42)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: getattr() takes no keyword arguments


I seem to recall that there was a reason for this, but I don't quite 
remember what... searching the archives may give a clue.

Personally, I have no objections *in principle* to adding support for 
keyword arguments to built-ins, but it will depend on the details of why 
they weren't already supported.



-- 
Steven


More information about the Python-ideas mailing list