What is functional programming
Functional programming treats computation as the evaluation of functions avoiding shared state and mutable data. Python isn’t a pure functional language, is a multi-paradigm language that supports functional programming, but it has excellent functional tools. Key FP tools in Python include:
lambda— anonymous, single-expression functionsmap(),filter(),reduce()— classic higher-order functions for transforming and aggregating data- List/dict/set comprehensions — a Pythonic, declarative way to build collections
functoolsmodule — utilities likefunctools.reduce(),functools.partial(), andfunctools.lru_cache()- Closures — functions that capture variables from their enclosing scope
- Immutability via convention — Python has no enforced immutability, but
tupleandfrozensetare immutable types
A quick example comparing styles:
# ❌ Imperative style — how to do it
total = 0
for x in [1, 2, 3, 4, 5]: if x % 2 == 0: total += x ** 2# ✅ Functional style — what you want
from functools import reduce
total = reduce(lambda acc, x: acc + x, map(lambda x: x**2, filter(lambda x: x % 2 == 0, [1, 2, 3, 4, 5])))
# ✅ Pythonic functional style — most readabletotal = sum(x**2 for x in [1, 2, 3, 4, 5] if x % 2 == 0)Another example:
numbers = [1, 2, 3, 4, 5]
# ❌ Imperative stylesquares = []for n in numbers: if n % 2 == 0: squares.append(n ** 2)numbers = [1, 2, 3, 4, 5]
# ✅ Functional stylesquares = list(map(lambda n: n ** 2, filter(lambda n: n % 2 == 0, numbers)))
# ✅ Pythonic functional style (comprehension)squares = [n ** 2 for n in numbers if n % 2 == 0]Python’s functional support is practical rather than pure, it doesn’t enforce immutability or guarantee tail-call optimization, so it’s best used selectively alongside OOP or procedural code rather than as an exclusive paradigm.
Quick Reference
Section titled “Quick Reference”| Concept | Tool | Use case |
|---|---|---|
| Transform | map(), comprehension | Apply function to every element |
| Filter | filter(), comprehension | Keep elements matching condition |
| Fold | reduce() | Collapse iterable to single value |
| Fix args | partial() | Pre-fill function arguments |
| Cache | lru_cache, cache | Memoize pure functions |
| Overload | singledispatch | Type-based dispatch |
| Fill comparisons | total_ordering | Define one, get all |
| Capture state | Closure | Stateful function factories |
| Combine functions | Composition | Build pipelines from small functions |
| Immutability | NamedTuple, frozen=True | Safe, predictable data |
| Iteration tools | itertools | Efficient, composable iteration |
The Truly Final Complete Advanced Python Journey
Section titled “The Truly Final Complete Advanced Python Journey”| Topic | Status |
|---|---|
| OOP & Dunder Methods | ✅ |
| Functional Programming | ✅ |
| Iterators & Generators | ✅ |
| Decorators | ✅ |
| Context Managers | ✅ |
| Advanced Error Handling | ✅ |
| Advanced Data Structures | ✅ |
| Concurrency & Async | ✅ |
| Metaclasses & Descriptors | ✅ |
| Memory & Performance | ✅ |
| Modules & Packaging | ✅ |
| Type Hints & Static Analysis | ✅ |