为什么 Python 不适合函数式编程? [英] Why isn't Python very good for functional programming?

查看:31
本文介绍了为什么 Python 不适合函数式编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为函数式编程可以用Python来完成.因此,我很惊讶 Python 没有在 this 问题,当它被提及时,通常不是很积极.但是,没有给出太多原因(提到了缺乏模式匹配和代数数据类型).所以我的问题是:为什么 Python 不适合函数式编程?除了缺乏模式匹配和代数数据类型之外,还有更多原因吗?或者这些概念对函数式编程如此重要,以至于不支持它们的语言只能被归类为二流函数式编程语言?(请记住,我在函数式编程方面的经验非常有限.)

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

推荐答案

您参考的问题询问哪些语言同时促进 OO 和函数式编程.Python 并不促进函数式编程,尽管它工作相当好.

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.

反对 Python 函数式编程的最佳论据是 Guido 仔细考虑了命令式/面向对象用例,而函数式编程用例则不然.当我编写命令式 Python 时,它是我所知道的最漂亮的语言之一.当我编写函数式 Python 时,它变得像没有 BDFL.

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.

这并不是说它不好,只是您必须比转而使用促进函数式编程的语言或转而编写面向对象的 Python 时更努力地工作.

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.

以下是我在 Python 中遗漏的功能性的东西:

Here are the functional things I miss in Python:

  • 没有模式匹配和尾递归意味着您必须强制编写基本算法.Python 中的递归既丑陋又缓慢.
  • 一个小的列表库和没有功能词典意味着你必须自己编写很多东西.
  • 没有柯里化或组合语法意味着无点样式与显式传递参数一样充满标点符号.
  • 迭代器而不是惰性列表意味着你必须知道你想要效率还是持久性,如果你想要持久性,就分散对 list 的调用.(迭代器使用一次)
  • Python 简单的命令式语法,连同其简单的 LL1 解析器,意味着为 if 表达式和 lambda 表达式提供更好的语法基本上是不可能的.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.

这篇关于为什么 Python 不适合函数式编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆