6

According to tutorialspoint.com, Python is a functional programming language. "Some of the popular functional programming languages include: Lisp, Python, Erlang, Haskell, Clojure, etc."

https://www.tutorialspoint.com/functional_programming/functional_programming_introduction.htm

But other sources say Python is an object-oriented programming language (you can create objects in Python).

So is Python both? If so, if you're trying to program something that requires lots of mathematical computations, would Python still be a good choice (Since functional languages have concurrency, better syntax for math, and higher-level functions)?

Jake Yoon
  • 120
  • 1
  • 5
  • 4
    Yes, Python can be used as both. – MattDMo Sep 14 '20 at 18:07
  • 7
    "*Python is a **multi-paradigm**, dynamically typed, multipurpose programming language*" (emphasis mine) - from [the description of the Python tag](https://stackoverflow.com/tags/python/info) – VLAZ Sep 14 '20 at 18:09
  • 4
    I really wouldn't consider Python a functional programming language. It has first-class functions, which allows you to use it as a functional programming language, and it borrows some nice features from functional programming languages, like list comprehensions, but it is fundamentally very imperative. Note, OOP lies on a different axis, really. Consider scala, which is very functional (although not purely), and yet, is also very OOP. – juanpa.arrivillaga Sep 14 '20 at 18:25
  • "If so, if you're trying to program something very mathematical and computational, would Python still be a good choice (As functional languages are more suitable for mathematical stuff)?" this is not a valid premise, I think, depending on what you mean by "mathematical stuff" and "computational". Do you mean things like numerical programming? Because you would either use a higher-level language designed for that, e.g. Matlab, or something close to the metal, e.g. C or fortran. Although note, `numpy` is a popular package for python that allows for fairly effective numeric programming in Python. – juanpa.arrivillaga Sep 14 '20 at 18:29
  • I think what you *may* mean is that mathematicians tend to like purely functional programming languages like Haskell and really like type theory and where the culture is often quite adjacent to pure/abstract mathematics, and terminology from category theory abounds. – juanpa.arrivillaga Sep 14 '20 at 18:34
  • @juanpa.arrivillaga I read that functional languages are better than OOP for things that require lots of mathematical functions and calculations such as game graphics and AI. Functional also allows concurrency. So I was wondering if python was a suitable language for such programming. – Jake Yoon Sep 14 '20 at 18:50
  • See also [Why isn't Python very good for functional programming?](https://stackoverflow.com/questions/1017621/why-isnt-python-very-good-for-functional-programming) – ggorlen Mar 28 '21 at 20:37

1 Answers1

21

Python, like many others, is a multi-paradigm language. You can use it as a fairly strictly imperative language, you can use it in a more object-oriented way, and you can use it in a more functional way. One important thing to note though is that functional is generally contrasted with imperative, object-oriented tends to exist at a different level and can be "layered over" a more imperative or a more functional core.

However Python is largely an imperative and object oriented language: much of the builtins and standard library are really built around classes and objects, and it doesn't encourage the sort of thinking which functional languages generally drive the user to.

In fact going through the (fairly terrible) list the article you link to provides, Python lands on the OOP side of more or less all of them:

  • it doesn't use immutable data much (it's not really possible to define immutable types in pure python, most of the collections are mutable, and the ones which are not are not designed for functional updates)
  • its execution model is very imperative
  • it has limited support for parallel programming
  • its functions very much do have side-effects
  • flow control is absolutely not done using function calls
  • it's not a language which encourages recursion
  • execution order is very relevant and quite strictly defined

Then again, much of the article is nonsense. If that is typical of that site, I'd recommend using something else.

If so, if you're trying to program something very mathematical and computational, would Python still be a good choice

Well Python is a pretty slow language in and of itself, but at the same time it has a very large and strong ecosystem of scientific libraries. It's probably not the premier language for abstract mathematics (it's rather bad at symbolic manipulation) but it tends to be a relatively good glue or prototyping tool.

As functional languages are more suitable for mathematical stuff

Not necessarily. But not knowing what you actually mean by "mathematical stuff" it's hard to judge. Do you mean symbolic manipulations? Statistics? Hard computations? Something else entirely?

Masklinn
  • 34,759
  • 3
  • 38
  • 57
  • 1
    my impression of tutorialspoint is that it isn't great for anything but the bare-bones basics, and even then, it can be wrong. It may have gotten better lately, but I seem to recall it would often promote various unidiomatic things in Python, at least. – juanpa.arrivillaga Sep 14 '20 at 18:36
  • I am trying to appy some of the oop that I know in java on python. Applying oop in a 100% fashion is so difficult – pentanol Feb 09 '21 at 18:31