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

Paul Moore p.f.moore at gmail.com
Sat Apr 7 04:48:20 EDT 2018


On 7 April 2018 at 08:44, Raymond Hettinger <raymond.hettinger at gmail.com> wrote:
> Agreed that the "chain([x], it)" step is obscure.  That's a bit of a bummer -- one of the goals for the itertools module was to be a generic toolkit for chopping-up, modifying, and splicing iterator streams (sort of a CRISPR for iterators).  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)
>
> Thanks for taking a look at the proposal.  I was -0 when it came up once before. Once I saw a use case pop-up on this list, I thought it might be worth discussing again.

I don't have much to add here - I typically agree that an explicit
loop is simpler, but my code tends not to be the sort that does this
type of operation, so my experience is either where it's not
appropriate, or where I'm unfamiliar with the algorithms, so terseness
is more of a problem to me than it would be to a domain expert.

Having said that, I find that the arguments that it's easy to add and
it broadens the applicability of the function to be significant.
Certainly, writing a helper is simple, but as Tim pointed out, the
trick to writing that helper is obscure. Also, in the light of the
itertools design goal to be a toolkit for iterators, I often find that
the tools are just slightly *too* low level for my use case - they are
designed to be combined, certainly, but in practice I find that
building my own loop is often quicker than working out how to combine
them. (I don't have concrete examples, unfortunately - this feeling
comes from working back from the question of why I don't use itertools
more than I do). So I tend to favour such slight extensions to the use
cases of itertools functions.

A recipe would help, but I don't know how much use the recipes see in
practice. I see a lot of questions where "there's a recipe for that"
is the answer - indicating that people don't always spot the recipes.

So I guess I'm +0 on the change - but as a matter of principle, rather
than from a pressing need for it, so don't take that vote too
seriously.

Paul


More information about the Python-ideas mailing list