Python's simplicity philosophy

Alex Martelli aleax at aleax.it
Fri Nov 14 13:54:54 CET 2003

```BW Glitch wrote:
...
> It wasn't obvious for me until later. reduce() is more likely to be used
> for optimization. IIRC, some said that optimization is the root of all
> evil.

That's *PREMATURE* optimization (and "of all evil IN PROGRAMMING", but
of the two qualifications this one may be less crucial here) -- just like
misquoting "The LOVE OF money is the root of all evil" as "MONEY is the
root of all evil", so does this particular misquote trigger me;-).

Optimization is just fine IN ITS PROPER PLACE -- after "make it work"
and "make it right", there MAY come a time where "make it fast" applies.

It's extremely unlikely that 'reduce' has a good place in an optimization
phase, of course -- even when some operator.foo can be found:

[alex at lancelot tmp]\$ timeit.py -c -s'import operator' -s'xs=range(1,321)'
'r=reduce(operator.mul, xs)'
1000 loops, best of 3: 450 usec per loop

[alex at lancelot tmp]\$ timeit.py -c -s'import operator' -s'xs=range(1,321)'
'r=1' 'for x in xs: r*=x'
1000 loops, best of 3: 440 usec per loop

reduce shows no advantage compared with a perfectly plain loop, and
when no operator.bar is around and one must use lambda:

[alex at lancelot tmp]\$ timeit.py -c -s'import operator' -s'xs=range(1,321)'
'r=reduce(lambda x, y: pow(x, y, 100), xs)'
1000 loops, best of 3: 650 usec per loop

[alex at lancelot tmp]\$ timeit.py -c -s'import operator' -s'xs=range(1,321)'
'r=1' 'for x in xs: r = pow(r, x, 100)'
1000 loops, best of 3: 480 usec per loop

reduce gets progressively slower and slower than the pian loop.  It's
just no bloody good, except maybe for people short-sighted enough to
believe that it's "more concise" than the loop (check out the lengths
of the timed statements above to dispel THAT myth) and who'd rather
slow things down by (say) 35% than stoop to writing a shorter, plainer
loop that every mere mortal would have no trouble understanding.

> Just because it's _obvious_ to you, it doesn't mean it's obvious to
> people who self taught programming.

That may be the real motivation for the last-ditch defenders of reduce:
it's one of the few (uselessly) "clever" spots in Python (language and
built-ins) where they may show off their superiority to mere mortals,
happily putting down as sub-human anybody who doesn't "get" higher-order
functions in 10 seconds flat (or less)...;-)

Alex

```