- Mastering Python
- Rick van Hattem
- 268字
- 2021-07-16 11:10:34
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
comprehensionsdict
comprehensionsset
comprehensionslambda
functionsfunctools
(partial
, andreduce
)itertools
(accumulate
,chain
,dropwhile
,starmap
, and so on)
- Apache Karaf Cookbook
- Oracle 18c 必須掌握的新特性:管理與實戰
- Visual C++開發入行真功夫
- Python機器學習算法: 原理、實現與案例
- Java零基礎實戰
- 深入理解BootLoader
- Maven for Eclipse
- 高效使用Greenplum:入門、進階與數據中臺
- 和孩子一起學編程:用Scratch玩Minecraft我的世界
- Raspberry Pi開發實戰
- Apache Kafka 1.0 Cookbook
- FORTRAN程序設計權威指南
- C++游戲設計案例教程
- JavaScript實戰-JavaScript、jQuery、HTML5、Node.js實例大全(第2版)
- HTML5從入門到精通(第3版)