Functional programming can be done in Python, I’ve always believed. As a result, I was surprised that Python didn’t get much of a mention in this topic, and when it did, it was usually in a negative light. However, not many reasons were given for this (lack of pattern matching and algebraic data types were mentioned). So my question is: why isn’t Python very good for functional programming? Are there more reasons than its lack of pattern matching and algebraic data types? Or are these concepts so important to functional programming that a language that doesn’t support them can only be classed as a second rate functional programming language? (Keep in mind that my experience with functional programming is quite limited.)
Asked by David Johnstone
Which languages promote both OO and functional programming, according to the question you reference? Despite the fact that it works effectively, Python does not encourage functional programming.
The biggest argument against functional programming in Python is that Guido carefully considers imperative/OO use cases, but not functional programming use cases. It’s one of the finest languages I know when I write imperative Python. When I create functional Python, it becomes just as ugly and unattractive as any other language without a BDFL.
That’s not to suggest it’s terrible; it just means you’ll have to put in more effort than if you went to a language that encourages functional programming or started writing OO Python.
Here are the features of Pyth that I miss.
Answered by Nathan Shively-Sanders
This is explained well by Guido here. The following is the most important part:
I take two things away from this:
Answered by Jason Baker
Although Scheme lacks algebraic data types and pattern matching, it is unquestionably a functional language. Python’s annoyances from the standpoint of functional programming:
On the other hand, lexical closures, Lambdas, and list comprehensions are all available in Python (which are really a “functional” concept whether or not Guido admits it). In Python, I do a lot of “functional-style” programming, although it’s far from perfect.
Answered by Jacob B
Although I would never call Python “functional,” the code I write in it invariably ends up being almost entirely functional.
That is, admittedly, largely due to the excellent list comprehension. So, while I wouldn’t recommend Python as a functional programming language, I would recommend functional programming to anyone who uses Python.
Answered by Konrad Rudolph
Let me show you how with some code from a SO answer to a “functional” Python query.
def grandKids(generation, kidsFunc, val): layer = [val] for i in xrange(generation): layer = itertools.chain.from_iterable(itertools.imap(kidsFunc, layer)) return layer
grandKids generation kidsFunc val = iterate (concatMap kidsFunc) [val] !! generation
The main difference here is that Haskell’s standard library has useful functions for functional programming: in this case iterate, concat, and (!!)
Answered by yairchu
Post is based on https://stackoverflow.com/questions/1017621/why-isnt-python-very-good-for-functional-programming