The Python programming languages contains some features from the functional programming paradigm. Of course, it is not primarily a functional language: many things are simply clearer when written out as a loop. The documentation about statements in lambdas also seems to have a pretty clear opinion on the topic:
Unlike lambda forms in other languages, where they add functionality, Python lambdas are only a shorthand notation if you’re too lazy to define a function.
Nonetheless, Python has some handy functional features. Functions are first-class citizens, we have various comprehensions, and in particular, we have some higher order functions such as map
and filter
in the builtins.
But not reduce
. It appears that it was banished from the builtins to the functools
module with the advent of Python 3.0. Guido expresses in the same post that he also wanted to get rid of map
and filter
, but perhaps due to the fact that those two could be rewritten to be lazily evaluated they were allowed to continue to exist?
I understand that reduce
is not that popular. In many cases, code written with a reduce
are less obvious than code written with an explicit loop and an accumulator. But this is just an opinion, something concerning esthetics, at the cost of consistency.
All three are higher order functions that work on an iterable. In fact, mathematically speaking, map
and filter
are special instances of a reduce
or fold. So why are map
and filter
builtins but not reduce
?
I’m not necessarily suggesting to move reduce
back into the builtins. To me, it feels more important that these functions that logically belong together are also, in fact, implemented together (i.e. live in the same namespace). Whether this is __builtins__
or functools
is secondary to me.
But, considering I also believe that the builtins should be kept as tiny as possible, that leaves me with the opinion that map
and filter
should be moved to the functools
module!
(Of course, I’m aware that this will never happen. But I would like to hear your thoughts.)