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.)
当前回答
In addition to other answers, one reason Python and most other multi-paradigm languages are not well suited for true functional programming is because their compilers / virtual machines / run-times do not support functional optimization. This sort of optimization is achieved by the compiler understanding mathematical rules. For example, many programming languages support a map function or method. This is a fairly standard function that takes a function as one argument and a iterable as the second argument then applies that function to each element in the iterable.
不管怎样,map(foo(), x) * map(foo(), y)和map(foo(), x * y)是一样的。后者实际上比前者快,因为前者执行两个副本,而后者执行一个副本。
更好的函数式语言能够识别这些基于数学的关系,并自动执行优化。不致力于函数式范式的语言可能无法优化。
其他回答
让我用一段代码来演示,这段代码摘自一个关于SO的“函数式”Python问题的答案
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
这里的主要区别是Haskell的标准库具有用于函数式编程的有用函数:在本例中是iterate、concat和(!!)
Python几乎是一种函数式语言。它是“功能精简版”。
它有额外的特性,所以对某些人来说不够纯。
它还缺乏一些功能,因此对某些人来说还不够完整。
缺少的特性相对容易编写。查看类似这样的关于Python中的FP的帖子。
Scheme没有代数数据类型或模式匹配,但它肯定是一种函数式语言。从函数式编程的角度来看,Python令人讨厌的地方:
λ。由于Lambdas只能包含一个表达式,并且不能在表达式上下文中轻松地做所有事情,这意味着可以“动态”定义的函数是有限的。 if是语句,不是表达式。这意味着,除其他外,你不能有一个包含If的lambda。(这在Python 2.5中由三元组修复,但看起来很难看。) Guido每隔一段时间就威胁要移除map、filter和reduce
另一方面,python有词法闭包、Lambdas和列表推导式(无论Guido是否承认,这实际上是一个“函数式”概念)。我用Python做了很多“函数式”编程,但我不敢说它是理想的。
对于这个问题(以及答案),有一点非常重要: 函数式编程到底是什么,它最重要的特性是什么? 我将尝试给出我的观点:
Functional programming is a lot like writing math on a whiteboard. When you write equations on a whiteboard, you do not think about an execution order. There is (typically) no mutation. You don't come back the day after and look at it, and when you make the calculations again, you get a different result (or you may, if you've had some fresh coffee :)). Basically, what is on the board is there, and the answer was already there when you started writing things down, you just haven't realized what it is yet.
函数式编程与此很相似;你不需要改变,只需要评估 方程(或者在这种情况下,“程序”),并找出答案是什么。这个项目 还在那里,没有改变。数据也是一样。
I would rank the following as the most important features of functional programming: a) referential transparency - if you evaluate the same statement at some other time and place, but with the same variable values, it will still mean the same. b) no side effect - no matter how long you stare at the whiteboard, the equation another guy is looking at at another whiteboard won't accidentally change. c) functions are values too. which can be passed around and applied with, or to, other variables. d) function composition, you can do h=g·f and thus define a new function h(..) which is equivalent to calling g(f(..)).
这个列表是按我的优先顺序排列的,所以参考透明度是最重要的, 而且没有副作用。
现在,如果你浏览python并检查该语言和库的支持程度, 并保证,这些方面,然后你就可以很好地回答你自己的问题。
我从来不会称Python为“函数式”,但无论何时我用Python编程,代码总是几乎完全是函数式的。
不可否认,这主要是由于非常好的列表理解。所以我不一定建议Python作为函数式编程语言,但我建议使用Python的人进行函数式编程。
推荐文章
- 将Pandas或Numpy Nan替换为None以用于MysqlDB
- 使用pandas对同一列进行多个聚合
- 使用Python解析HTML
- django MultiValueDictKeyError错误,我如何处理它
- 如何在for循环期间修改列表条目?
- 我如何在Django中创建一个鼻涕虫?
- 没有名为'django.core.urlresolvers'的模块
- 蟒蛇导出环境文件
- Django - makemigrations -未检测到任何更改
- SQLAlchemy:引擎、连接和会话差异
- 在Python Pandas中删除多个列中的所有重复行
- 更改pandas DataFrame中的特定列名
- 将Pandas多索引转换为列
- 熊猫在每组中获得最高的n个记录
- 熊猫数据帧得到每组的第一行