Summary
In this chapter, we looked at alternatives to the if
, elif
, and else
statement sequence. Ideally, using a conditional expression allows some optimization to be done. Pragmatically, Python doesn't optimize, so there's little tangible benefit to the more exotic ways to handle conditions.
We also looked at how we can use the operator
module with higher-order functions such as max()
, min()
, sorted()
, and reduce()
. Using operators can save us from having to create a number of small lambdas.
In the next chapter, we'll look at the PyMonad
library to express a functional programming concept directly in Python. We don't require monads generally because Python is an imperative programming language under the hood.
Some algorithms might be expressed more clearly with monads than with stateful variable assignments. We'll look at an example where monads lead to a succinct expression of a rather complex set of rules. Most importantly, the operator
module shows off many functional programming techniques...