[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:19:50 EDT 2018
On 8 April 2018 at 14:31, Guido van Rossum <guido at python.org> wrote:
> Given that two respected members of the community so strongly disagree
> whether accumulate([], start=0) should behave like accumulate([]) or like
> accumulate([0]), maybe in the end it's better not to add a start argument.
> (The disagreement suggests that we can't trust users' intuition here.)
The potential ambiguity I see is created mainly by calling the
proposed parameter "start", while having it do more than just adjust
the individual partial sum calculations by adding an extra partial
result to the output series.
If it's called something else (e.g. "first_result"), then the
potential "sum(iterable, start=start)" misinterpretation goes away,
and it can have Tim's desired effect of defining the first output
value (effectively prepending it to the input iterable, without the
boilerplate and overhead of actually doing so).
A name like "first_result" would also make it clearer to readers that
passing that parameter has an impact on the length of the output
series (since you're injecting an extra result), and also that the
production of the first result skips calling func completely (as can
be seen in Tim's str coercion example).
So where I'd be -1 on:
>>> list(accumulate(1, 2, 3))
[1, 3, 6]
>>> list(accumulate(1, 2, 3, start=0))
[0, 1, 3, 6]
>>> list(accumulate(1, 2, 3, start=1))
[1, 2, 4, 7]
I'd be +1 on:
>>> list(accumulate(1, 2, 3))
[1, 3, 6]
>>> list(accumulate(1, 2, 3, first_result=0))
[0, 1, 3, 6]
>>> list(accumulate(1, 2, 3, first_result=1))
[1, 2, 4, 7]
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list