Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I have always thought that functional programming can be done in Python. Thus, I was surprised that Python didn't get much of a mention in this question, and when it was mentioned, it normally wasn't very positive. 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.)

1 Answer

0 votes
by (106k points)

The reason why Python is not good for functional programming is that at the time of writing the language the inventor Guido considered the imperative use cases, while functional programming use cases are not covered. When you write imperative Python, then Python becomes one of the prettiest languages.

Below is a list of some functional things which you will miss in Python:

  • Pattern matching.

  • Tail recursion

  • Large library of list functions

  • Functional dictionary class

  • Automatic currying

  • A concise way to compose functions

  • Lazy lists

  • The meaning of no pattern matching and no tail recursion in Python is your basic algorithms have to be written imperatively. Recursion is ugly and slow in Python.

  • There is no syntax for currying or composition means that point-free style is about as full of punctuation as explicitly passing arguments.

  • Iterators instead of lazy lists mean that you have to know whether you want efficiency or persistence and to scatter calls to list around if you want persistence. (Iterators are use-once)

Browse Categories

...