[Python-ideas] Start argument for itertools.accumulate() [Was: Proposal: A Reduce-Map Comprehension and a "last" builtin]

Nick Coghlan ncoghlan at gmail.com
Sun Apr 8 01:26:01 EDT 2018


On 8 April 2018 at 15:00, Tim Peters <tim.peters at gmail.com> wrote:
> `accumulate()` accepts any two-argument function.
>
>>>> itertools.accumulate([1, 2, 3], lambda x, y: str(x) + str(y))
> <itertools.accumulate object at 0x0000028AB1B3B448>
>>>> list(_)
> [1, '12', '123']
>
> Arguing that it "has to do" something exactly the way `sum()` happens
> to be implemented just doesn't follow - not even if they happen to
> give the same name to an optional argument.  If the function were
> named `accumulate_sum()`, and restricted to numeric types, maybe - but
> it's not.

When first added in 3.2, it did have that restriction, and the default
behaviour without a function argument still parallels repeated use of
the sum() builtin.

Extending the parallel to *also* include a parameter called "start"
would create the expectation for me that that parameter would adjust
the partial result calculations, without adding an extra result.

Other parameter names (like the "first_result" I suggested in my reply
to Guido) wouldn't have the same implications for me, so this
objection is specific to the use of "start" as the parameter name, not
to the entire idea.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list