Supplementing the std lib (Was: partial sums problem)

kj no.email at please.post
Wed Sep 29 15:54:18 CEST 2010


In <mailman.1142.1285722789.29448.python-list at python.org> Terry Reedy <tjreedy at udel.edu> writes:

>Do not try to do a reduction with a comprehension. Just write clear, 
>straightforward code that obviously works.

>s=[1,2,3,4,5,6]
>def cusum(s):
>   t = 0
>   for i in s:
>       t += i
>       yield t

>print(list(cusum(s)))
> >>>
>[1, 3, 6, 10, 15, 21]


Actually, this is just fine.  Thank you!

But it brings up a new question, of an entirely different nature.

It's a recurrent conundrum, actually, and I have not found a good
solution.

Your cusum function is one that I would like to see somewhere in
the standard library (in itertools maybe?).  Maybe some future
version of the standard library will have it, or something like it
(I'm thinking of a generalized form which, like reduce, takes a
function and an initial value as arguments).

But in the immediate term, cusum is not part of the standard library.

Where would you put it if you wanted to reuse it?  Do you create
a module just for it?  Or do you create a general stdlib2 module
with all those workhorse functions that have not made it to the
standard library?  Or something else entirely?

(I'm not expecting to get *the* solution from anyone reply; rather,
I'm interested in reading people's take on the question and their
way of dealing with those functions they consider worthy of the
standard library.)

kj




More information about the Python-list mailing list