10. Functional Programming#
Data science work is mostly transformation: clean raw records, convert text to numbers, keep the rows that matter, and rank or summarize the result. When these steps change shared data in place or depend on hidden state, a result becomes hard to reproduce and a bug becomes hard to trace. Functional programming organizes the same work as transformations from inputs to outputs, so each step can be tested on its own and rerun with the same result. In Chapter 9, objects managed state; this chapter adds the complementary tools for calculations.
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.
Chapter Video
This 15-minute video introduces the idea that functions are values that can be assigned, passed, and returned.
Programming Terms: First-Class Functions (15:28) by Corey Schafer
Hosted on YouTube; property of its original creator, embedded here for educational reference only. As you watch, notice where a function is passed as an argument, and compare it with the
map()andsorted()examples in 1001.
Learning Objectives
By the end of this chapter, you will be able to:
Classify functions as pure or impure, and rewrite functions that mutate their inputs so that they return new values
Pass, return, and wrap functions with lambda expressions,
map(),filter(),sorted(),reduce(), closures, and decoratorsRewrite
map()andfilter()pipelines as comprehensions, choose between the two styles, and refactor a loop into a pipeline of small pure stepsCompare recursion with iteration, and write recursive functions for nested data
Write context managers with
@contextmanager, and usepartial()andlru_cache()fromfunctoolswhere they are safe
Chapter Flow
Glossary
# |
Term |
Explanation |
|---|---|---|
1 |
Functional programming |
A programming style that emphasizes functions, expressions, and limited mutation. |
2 |
Pure function |
A function whose output depends only on its inputs and that has no side effects. |
3 |
Side effect |
A change or interaction outside a function’s returned value, such as printing or mutating data. |
4 |
Immutability |
The practice of using values that are not changed after creation. |
5 |
First-class function |
A function that can be stored, passed as an argument, or returned like other values. |
6 |
Higher-order function |
A function that accepts another function as an argument or returns a function. |
7 |
Closure |
A function that remembers values from the scope where it was created, even after that scope has finished running. |
8 |
Lambda function |
A small anonymous function written with the |
9 |
Map |
A pattern that transforms each item in a collection. |
10 |
Filter |
A pattern that keeps only items that satisfy a condition. |
11 |
Reduce |
A pattern that combines many values into one result. |
12 |
|
The optional starting value of the accumulator in |
13 |
Decorator |
A function that wraps another function to extend or change its behavior. |
14 |
Comprehension |
A compact expression for building a collection from an iterable. |
15 |
Recursion |
A technique where a function calls itself on a smaller version of a problem. |
16 |
Base case |
The stopping condition in a recursive function. |
17 |
Recursive case |
The part of a recursive function that calls the function again. |
18 |
Context manager |
An object that manages setup and cleanup around a |
19 |
|
A standard-library module with tools for functional programming patterns. |
20 |
Partial function |
A function with some arguments pre-filled, often created with |
21 |
Memoization |
Saving a function’s results by argument so that a repeated call returns the saved result instead of recomputing it. |
22 |
Cached result |
A saved function result returned immediately when the same arguments are used again. |
23 |
|
A |