官术网_书友最值得收藏!

Chapter 4. Functional Programming – Readability Versus Brevity

Python is one of the few (or at least the earliest) nonfunctional languages to incorporate functional features. While Guido van Rossum has tried to remove some of them a few times, they have become ingrained in the Python community, and list comprehensions (dict and set comprehensions soon to follow) are widely used in all sorts of code. The most important thing about code shouldn't be how cool your reduce statement is or how you can fit the entire function in a single line with an incomprehensible list comprehension. Readability counts (once again, PEP20)!

This chapter will show you some of the cool tricks that functional programming in Python gives you, and it will explain some of the limitations of Python's implementation. While we will try to steer clear of lambda calculus (λ-calculus) as much as possible, the Y combinator will be discussed briefly.

The last few paragraphs will list (and explain) the usage of the functools and itertools libraries. If you are familiar with these libraries, feel free to skip them, but note that some of these will be used heavily in the later chapters about decorators (Chapter 5, Decorators – Enabling Code Reuse by Decorating), generators (Chapter 6, Generators and Coroutines – Infinity, One Step at a Time), and performance (Chapter 12, Performance – Tracking and Reducing Your Memory and CPU Usage).

These are the topics covered in this chapter:

  • The theory behind functional programming
  • list comprehensions
  • dict comprehensions
  • set comprehensions
  • lambda functions
  • functools (partial, and reduce)
  • itertools (accumulate, chain, dropwhile, starmap, and so on)
主站蜘蛛池模板: 来凤县| 广东省| 桦甸市| 盐津县| 利辛县| 中阳县| 中江县| 潢川县| 银川市| 抚远县| 水城县| 九台市| 新巴尔虎右旗| 黎平县| 牡丹江市| 嘉义县| 乳源| 偏关县| 乌海市| 赤峰市| 文化| 榆社县| 武强县| 沙田区| 西乡县| 遵义市| 玉树县| 色达县| 长阳| 普兰店市| 武安市| 左权县| 佳木斯市| 永清县| 西昌市| 潍坊市| 福建省| 抚宁县| 周口市| 外汇| 原阳县|