Functional Python Programming
上QQ阅读APP看书,第一时间看更新

Introducing Essential Functional Concepts

Most of the features of functional programming are already first-class parts of Python. Our goal in writing functional Python is to shift our focus away from imperative (procedural or object-oriented) techniques to as much of an extent as possible.

We'll look at each of the following functional programming topics:

  • First-class and higher-order functions, which are sometimes known as pure functions.
  • Immutable data.
  • Strict and non-strict evaluation. We can also call this eager versus lazy evaluation.
  • Recursion instead of an explicit loop state.
  • Functional type systems.

This should reiterate some concepts from the first chapter: firstly, that purely functional programming avoids the complexities of an explicit state maintained through variable assignments; secondly, that Python is not a purely functional language.

This book doesn't attempt to offer a rigorous definition of what functional programming is: Python is not a purely functional language, and a strict definition isn't helpful. Instead, we'll identify some common features that are indisputably important in functional programming. We'll steer clear of the blurry edges, and focus on the obviously functional features.

In this chapter, we'll include some of the Python 3 type hints in the examples. Type hints can help you visualize the essential purpose behind a function definition. Type hints are analyzed with the mypy tool. As with unit testing and static analysis with pylint, mypy can be part of a toolchain used to produce high-quality software.