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.)


当前回答

Guido对此有很好的解释。以下是最相关的部分:

I have never considered Python to be heavily influenced by functional languages, no matter what people say or think. I was much more familiar with imperative languages such as C and Algol 68 and although I had made functions first-class objects, I didn't view Python as a functional programming language. However, earlier on, it was clear that users wanted to do much more with lists and functions. ... It is also worth noting that even though I didn't envision Python as a functional language, the introduction of closures has been useful in the development of many other advanced programming features. For example, certain aspects of new-style classes, decorators, and other modern features rely upon this capability. Lastly, even though a number of functional programming features have been introduced over the years, Python still lacks certain features found in “real” functional programming languages. For instance, Python does not perform certain kinds of optimizations (e.g., tail recursion). In general, because Python's extremely dynamic nature, it is impossible to do the kind of compile-time optimization known from functional languages like Haskell or ML. And that's fine.

我从中得出两点结论:

该语言的创造者并不真正认为Python是一种函数式语言。因此,可能会看到“函数式”的特性,但不太可能看到任何明确的功能。 Python的动态特性抑制了您在其他函数式语言中看到的一些优化。当然,Lisp和Python一样是动态的(如果不是更动态的话),所以这只是部分解释。

其他回答

Guido对此有很好的解释。以下是最相关的部分:

I have never considered Python to be heavily influenced by functional languages, no matter what people say or think. I was much more familiar with imperative languages such as C and Algol 68 and although I had made functions first-class objects, I didn't view Python as a functional programming language. However, earlier on, it was clear that users wanted to do much more with lists and functions. ... It is also worth noting that even though I didn't envision Python as a functional language, the introduction of closures has been useful in the development of many other advanced programming features. For example, certain aspects of new-style classes, decorators, and other modern features rely upon this capability. Lastly, even though a number of functional programming features have been introduced over the years, Python still lacks certain features found in “real” functional programming languages. For instance, Python does not perform certain kinds of optimizations (e.g., tail recursion). In general, because Python's extremely dynamic nature, it is impossible to do the kind of compile-time optimization known from functional languages like Haskell or ML. And that's fine.

我从中得出两点结论:

该语言的创造者并不真正认为Python是一种函数式语言。因此,可能会看到“函数式”的特性,但不太可能看到任何明确的功能。 Python的动态特性抑制了您在其他函数式语言中看到的一些优化。当然,Lisp和Python一样是动态的(如果不是更动态的话),所以这只是部分解释。

Python几乎是一种函数式语言。它是“功能精简版”。

它有额外的特性,所以对某些人来说不够纯。

它还缺乏一些功能,因此对某些人来说还不够完整。

缺少的特性相对容易编写。查看类似这样的关于Python中的FP的帖子。

Scheme没有代数数据类型或模式匹配,但它肯定是一种函数式语言。从函数式编程的角度来看,Python令人讨厌的地方:

λ。由于Lambdas只能包含一个表达式,并且不能在表达式上下文中轻松地做所有事情,这意味着可以“动态”定义的函数是有限的。 if是语句,不是表达式。这意味着,除其他外,你不能有一个包含If的lambda。(这在Python 2.5中由三元组修复,但看起来很难看。) Guido每隔一段时间就威胁要移除map、filter和reduce

另一方面,python有词法闭包、Lambdas和列表推导式(无论Guido是否承认,这实际上是一个“函数式”概念)。我用Python做了很多“函数式”编程,但我不敢说它是理想的。

您引用的问题是哪些语言同时促进面向对象和函数式编程。Python并不提倡函数式编程,尽管它工作得相当好。

反对Python中函数式编程的最佳论据是Guido仔细考虑了命令式/OO用例,而函数式编程用例则没有。当我编写命令式Python时,它是我所知道的最漂亮的语言之一。当我编写函数式Python时,它变得像没有BDFL的普通语言一样丑陋和令人不快。

这并不是说它不好,只是说您必须比转换到促进函数式编程的语言或转换到编写OO Python时更加努力。

以下是我在Python中遗漏的函数性内容:

模式匹配 尾递归 大型列表函数库 函数式字典类 自动加脂法 简洁的组合函数的方法 懒惰的列表 简单、强大的表达式语法(Python的简单块语法阻止Guido添加它)


No pattern matching and no tail recursion mean your basic algorithms have to be written imperatively. Recursion is ugly and slow in Python. A small list library and no functional dictionaries mean that you have to write a lot of stuff yourself. 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 means 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) Python's simple imperative syntax, along with its simple LL1 parser, mean that a better syntax for if-expressions and lambda-expressions is basically impossible. Guido likes it this way, and I think he's right.

上面没有提到的另一个原因是,许多内置类型的内置函数和方法修改了对象,但不返回修改后的对象。如果返回这些修改后的对象,将使函数代码更干净、更简洁。例如,如果some_list.append(some_object)返回附加some_object的some_list。