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

Nick Coghlan ncoghlan at gmail.com
Sat Apr 7 22:31:38 EDT 2018


On 8 April 2018 at 08:09, Tim Peters <tim.peters at gmail.com> wrote:
[Raymond wrote]:
>> The docs probably need another recipe to show this pattern:
>>
>>         def prepend(value, iterator):
>>             "prepend(1, [2, 3, 4]) -> 1 2 3 4"
>>             return chain([value], iterator)
>
> +1.  Whether `accumulate()` should grow a `start=` argument still
> seems a distinct (albeit related) issue to me, though.

I didn't have a strong opinion either way until Tim mentioned sum()
and then I went and checked the docs for both that and for accumulate.

First sentence of the sum() docs:

    Sums *start* and the items of an *iterable* from left to right and
returns the total.

First sentence of the accumulate docs:

    Make an iterator that returns accumulated sums, ...

So I now think that having "start" as a parameter to one but not the
other, counts as a genuine API discrepancy.

Providing start to accumulate would then mean the same thing as
providing it to sum(): it would change the basis point for the first
addition operation, but it wouldn't change the *number* of cumulative
sums produced.

By contrast, using the prepend() approach with accumulate() not only
changes the starting value, it also changes the number of cumulative
sums produced.

Cheers,
Nick.

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


More information about the Python-ideas mailing list