franklin
franklin

Reputation: 1819

What makes Python not a functional programming language?

Python variables are duck typed, mutable and it's functions can be written to have side effects. (In other words, it has a lot of non-functional programming features.)

However, it also has first-class functions and yet it's not a functional programming language. So I guess it must be true that a programming language can have functional aspects (or aspects of another language paradigm) even if it does not fall into that paradigm. What exactly makes Python not a functional programming language? Is it a test of whether it has a single characteristic or more of a test of multiple things? (If the former, what is the single aspect that makes Python non-functional or the single aspect that makes Haskell functional?)

Upvotes: 3

Views: 2921

Answers (2)

Bhav Bhela
Bhav Bhela

Reputation: 347

A great post already on SO: Why isn't Python very good for functional programming?

The statement "Python is not a functional language" stems the language's design supporting other paradigms over a functional style [1]. That does not mean the language doesn't show functional characteristics as you have discerned. There is even official documentation that describes how you can leverage the python language to write code in a functional style.

Python is a multi-paradigm language and thus has a blend of attributes from each of paradigm it supports. Python can easily be considered one paradigm or not, much like deciding which paradigm is best, it is subject to the opinion of the individual.

For some interesting related readings, here is a SO post touching upon the general difficulty in what makes a language purely functional (for practical uses): Are there any purely functional Schemes or Lisps?

And this article explores the conflict of real world applications and the technicalities that contrast with functional programming paradigm: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.13.9123

[1] as it was initially designed, and as it is continued to be designed

Upvotes: 2

Carcigenicate
Carcigenicate

Reputation: 45826

Python is considered functional; if you can trust Wikipedia:

Paradigm Multi-paradigm: functional, imperative, object-oriented, reflective

Emphasis mine. This is from the summary side-bar.

And it should be noted that the "FP-ness" of a language isn't binary, it's on a continuum. Python doesn't have built in support for efficient manipulation of immutable structures as far as I know. That's one large knock against it, as immutability can be considered a strong aspect of FP. It also doesn't support tail-call optimization, which can be a problem when dealing with recursive solutions. As you mentioned though, it does have first-class functions, and built-in support for some idioms, such as map/comprehensions/generator expressions, reduce and lazy processing of data.

If you want to deal exclusively with functional programming idioms, Python may not be the best choice. That alone doesn't exclude it from being considered a functional language though.

Upvotes: 6

Related Questions