Functional Programming – Readability Versus Brevity
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. For learning and entertainment, we will also briefly discuss the mathematical equivalent using lambda calculus, using the Y combinator as an example.
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 6), generators (Chapter 7), and performance (Chapter 12).
These are the topics covered in this chapter:
- The theory behind functional programming
list
,dict
, andset
comprehensionslambda
functionsfunctools
(partial
andreduce
)itertools
(accumulate
,chain
,dropwhile
,starmap
, and so on)
First...