推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
momentum
V2EX  ›  Python

在 python 编程中大量使用函数式风格,推荐吗?

  •  
  •   momentum · Dec 1, 2016 · 4594 views
    This topic created in 3593 days ago, the information mentioned may be changed or developed.

    近半年读了较多函数式编程的书,算是彻底迷上了函数式编程,由于自己用 python 比较多,现在在 python 编程中也喜欢使用大量的函数式编程风格,有时可以写得很优美,有时则会显得比较令人费解。很多文章说 python 并不是一门很好的函数式编程语言,函数式编程风格也不那么的 pythonic ,所以不推荐在 python 中大量使用函数式风格。所以,大家怎么看这个问题?

    11 replies  •  2016-12-02 23:16:47 +08:00
    metaquant
        1
    metaquant  
       Dec 1, 2016   ❤️ 2
    严格来说, python 并不是一门函数式编程语言,祖师爷 Guido 实际上并不喜欢函数式风格,现在的 PSF 对函数式风格的支持也不是很情愿,只是由于群众的呼声,才加上了一些常见的函数式编程特性,就这样, python 3 里还把 reduce 移到 functools 模块里了。

    但是不可否认,函数式编程风格在解决了某些问题,特别是科学计算类的问题是非常自然的,科学计算有时只是将数学公式翻译为编程语言而已,这时,更接近数学语言的函数式编程风格更加适用与自然。在这些工作中,大量使用函数式编程风格是可行的,别人也更好理解。

    在 python 中运用函数式编程,除开最基本 map, filter, reduce 几个函数要熟练外,最关键提要理解高阶函数的概念。函数式编程的核心理论就是将大问题拆分为小问题,直到拆解成某个容易解决的原始问题,这时高阶函数便起到了核心作用。

    关于 python 中高阶函数的介绍,可以参见这篇译文: http://metaquant.org/python-func.html

    关于如何在 python 中使用函数式编程风格,可以参考这篇文章: https://docs.python.org/2/howto/functional.html

    总之,是否可以大量使用函数式编程风格,还是视你的编程领域而定,毕竟 python 是一门通用性语言,在不同的社区中的的最佳实践是大相径庭的。个人意见,仅供参考。
    westworld
        2
    westworld  
       Dec 1, 2016
    @metaquant 有道理,我也感觉编程风格方面不能钻牛角尖,写出健壮、易读、直观的代码才是最重要的
    momentum
        3
    momentum  
    OP
       Dec 1, 2016
    @westworld 这里存在一个度的问题,比较难把握
    alphabeta
        4
    alphabeta  
       Dec 1, 2016
    不推荐,要学函数式编程,建议直接上 Haskell ,才能学到函数式编程的精髓
    stamaimer
        5
    stamaimer  
       Dec 1, 2016 via iPhone
    同意二楼
    thekoc
        6
    thekoc  
       Dec 1, 2016
    个人觉得不适合。首先, Python 的递归默认有深度限制,并且不支持尾递归。另外, Python 的 lambda 函数不能换行,这个也比较麻烦。

    再看看函数式编程最为著名的三个工具: map , filter , reduce 。先说 reduce ,在 Python 2 中, reduce 是内建函数,但是在 Python 3 中却放到了 functools 模块里。再看 map 和 filter , Python 中完全被列表推导式替代了,可以说几乎没什么地位。

    另外,再看看 Python 创始人对递归的一些评论:
    > Third, I don't believe in recursion as the basis of all programming. This is a fundamental belief of certain computer scientists, especially those who love Scheme and like to teach programming by starting with a "cons" cell and recursion. But to me, seeing recursion as the basis of everything else is just a nice theoretical approach to fundamental mathematics (turtles all the way down), not a day-to-day tool.

    虽然递归不是函数式编程的全部,但是字里行间可以感受出来他对函数式编程是不太友好的……所以 Python 的设计哲学中函数式编程可能也不会占太大的位置。

    文章链接:
    http://neopythonic.blogspot.jp/2009/04/tail-recursion-elimination.html
    ericls
        7
    ericls  
       Dec 1, 2016 via iPhone
    据说 Python function call 有 overhead
    strahe
        8
    strahe  
       Dec 1, 2016
    不推荐,别的不说,举个例子,我现在接手的项目就是大量的函数式,从代码的风格上来说很多与 python 的特性想违背,很不友好,(至少从大部分人的角度来看是这样的)
    zhuangzhuang1988
        9
    zhuangzhuang1988  
       Dec 2, 2016
    没必要, 不推荐。。
    msg7086
        10
    msg7086  
       Dec 2, 2016
    如果你想玩函数式,为什么不从 Ruby 开始玩起呢?强势安利一发 Ruby 。
    102400
        11
    102400  
       Dec 2, 2016
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1200 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 16:44 · PVG 00:44 · LAX 09:44 · JFK 12:44
    ♥ Do have faith in what you're doing.