10. Functional Programming#
Functional programming is a style of writing programs as transformations from inputs to outputs.
Pure functions and side effects
Immutable data and returned values
Functions as values
Comprehensions, lambdas, and higher-order functions
Decorators, recursion, context managers, and
functools
Python is not a purely functional language, but it includes functional tools that help you write concise, reusable, and testable code.
Video
This overview introduces the idea that functions are values that can be assigned, passed, and returned.
Python Tutorial: First-Class Functions by Corey Schafer
Learning Goals
By the end of this chapter, you will be able to:
Identify pure functions and avoid unnecessary side effects
Use immutability by returning new values instead of mutating existing objects
Treat functions as first-class values that can be passed to and returned from other functions
Use lambda functions with
map(),filter(), andsorted()Build decorators, recursive functions, context managers, and selected
functoolsutilities