[Python-ideas] Proposal: A Reduce-Map Comprehension and a "last" builtin
klahnakoski at mozilla.com
Sun Apr 8 13:41:44 EDT 2018
On 2018-04-05 21:18, Steven D'Aprano wrote:
> (I don't understand why so many people have such an aversion to writing
> functions and seek to eliminate them from their code.)
I think I am one of those people that have an aversion to writing functions!
I hope you do not mind that I attempt to explain my aversion here. I
want to clarify my thoughts on this, and maybe others will find
something useful in this explanation, maybe someone has wise words for
me. I think this is relevant to python-ideas because someone with this
aversion will make different language suggestions than those that don't.
Here is why I have an aversion to writing functions: Every unread
function represents multiple unknowns in the code. Every function adds
to code complexity by mapping an inaccurate name to specific
When I read code, this is what I see:
> x = you_will_never_guess_how_corner_cases_are_handled(a, b, c)
> y =
Not everyone sees code this way: I see people read method calls, make a
number of wild assumptions about how those methods work, AND THEY ARE
CORRECT! How do they do it!? It is as if there are some unspoken
convention about how code should work that's opaque to me.
For example before I read the docs on
itertools.accumulate(list_of_length_N, func), here are the unknowns I see:
* Does it return N, or N-1 values?
* How are initial conditions handled?
* Must `func` perform the initialization by accepting just one
parameter, and accumulate with more-than-one parameter?
* If `func` is a binary function, and `accumulate` returns N values,
what's the Nth value?
* if `func` is a non-cummutative binary function, what order are the
* Maybe accumulate expects func(*args)?
* Is there a window size? Is it equal to the number of arguments of `func`?
These are not all answered by reading the docs, they are answered by
reading the code. The code tells me the first value is a special case;
the first parameter of `func` is the accumulated `total`; `func` is
applied in order; and an iterator is returned. Despite all my
questions, notice I missed asking what `accumulate` returns? It is the
unknown unknowns that get me most.
So, `itertools.accumulate` is a kinda-inaccurate name given to a
specific functionality: Not a problem on its own, and even delightfully
useful if I need it often.
What if I am in a domain where I see `accumulate` only a few times a
year? Or how about a program that uses `accumulate` in only one place?
For me, I must (re)read the `accumulate` source (or run the caller
through the debugger) before I know what the code is doing. In these
cases I advocate for in-lining the function code to remove these
unknowns. Instead of an inaccurate name, there is explicit code. If we
are lucky, that explicit code follows idioms that make the increased
verbosity easier to read.
Consider Serhiy Storchaka's elegant solution, which I reformatted for
> smooth_signal = [
> for average in 
> for x in signal
> for average in [(1-decay)*average + decay*x]
We see the initial conditions, we see the primary function, we see how
the accumulation happens, we see the number of returned values, and we
see it's a list. It is a compact, easy read, from top to bottom. Yes, we
must know `for x in [y]` is an idiom for assignment, but we can reuse
that knowledge in all our other list comprehensions. So, in the
specific case of this Reduce-Map thread, I would advocate using the list
In general, all functions introduce non-trivial code debt: This debt is
worth it if the function is used enough; but, in single-use or rare-use
cases, functions can obfuscate.
Thank you for your time.
More information about the Python-ideas