Problem
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, there were few explanations presented for this (lack of pattern matching and algebraic data types were mentioned). So, why isn’t Python a good language for functional programming? Are there any more factors besides pattern matching and algebraic data types? Or are these notions so crucial to functional programming that a language that lacks them can only be considered a second-rate functional programming language? (Keep in mind that my functional programming knowledge is limited.)
Asked by David Johnstone
Solution #1
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 Python that I miss:
Answered by Nathan Shively-Sanders
Solution #2
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
Solution #3
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
Solution #4
I would never call Python “functional” but whenever I program in Python the code invariably ends up being almost purely 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
Solution #5
Let me show you how with some code from a SO answer to a “functional” Python query.
Python:
def grandKids(generation, kidsFunc, val):
layer = [val]
for i in xrange(generation):
layer = itertools.chain.from_iterable(itertools.imap(kidsFunc, layer))
return layer
Haskell:
grandKids generation kidsFunc val =
iterate (concatMap kidsFunc) [val] !! generation
The key distinction is that Haskell’s standard library includes functional programming methods such as iterate, concat, and (!!).
Answered by yairchu
Post is based on https://stackoverflow.com/questions/1017621/why-isnt-python-very-good-for-functional-programming