本文介绍了为什么Python不适合函数式编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我一直认为可以用Python完成函数式编程。因此,我很惊讶Python没有在这个问题,当提到时,它通常不是很积极。然而,没有给出很多理由(缺少模式匹配和代数数据类型)。所以我的问题是:为什么Python不是很适合函数式编程?是否有比缺少模式匹配和代数数据类型更多的原因?或者这些概念对函数式编程如此重要,以至于不支持它们的语言只能被归类为二次函数式编程语言? (请记住,我在函数式编程方面的经验是相当有限的。)解决方案和功能编程。尽管Python函数很好地运行,但它并没有促进函数式编程。 最好的参数 / em> Python中的函数式编程是Guido严格考虑命令/ OO用例,而函数式编程用例则不是。当我编写命令式Python时,它是我认识的最漂亮的语言之一。当我编写函数式Python时,它会变得和一般语言一样丑陋和不愉快,因为它没有 BDFL 。 这并不是说它很糟糕,只是因为如果你改用一种语言来促进功能性编程或切换到编写OO Python。 以下是我在Python中错过的功能: 模式匹配 尾递归 列表功能的大型库 功能词典课程 自动卷曲 简明的撰写功能 简单,强大的表达式语法(Python的简单块语法可以防止Guido添加它) 没有模式匹配,也没有尾递归意味着您的基本算法必须写入命令。递归在Python中是丑陋而缓慢的。 一个小的列表库和没有功能的字典意味着你必须自己写很多东西。 没有用于currying或组合的语法意味着无点式样与充满标点符号的显式传递参数一样。 迭代器而不是懒惰列表意味着你必须知道你是否想要效率或持久性,并且如果你想持久化的话,将调用分散到 list 周围。 (迭代器是一次使用的) Python的简单命令语法及其简单的LL1解析器意味着更好的if-expressions和lambda表达式语法基本上是不可能的。 Guido喜欢这样,我认为他是对的。 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.) 解决方案 The question you reference asks which languages promote both OO and functional programming. Python does not promote functional programming even though it works fairly well.The best argument against functional programming in Python is that imperative/OO use cases are carefully considered by Guido, while functional programming use cases are not. When I write imperative Python, it's one of the prettiest languages I know. When I write functional Python, it becomes as ugly and unpleasant as your average language that doesn't have a BDFL.Which is not to say that it's bad, just that you have to work harder than you would if you switched to a language that promotes functional programming or switched to writing OO Python.Here are the functional things I miss in Python:Pattern matchingTail recursionLarge library of list functionsFunctional dictionary classAutomatic curryingConcise way to compose functionsLazy listsSimple, powerful expression syntax (Python's simple block syntax prevents Guido from adding it)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. 这篇关于为什么Python不适合函数式编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-21 15:42