liveBook · Manning

Functional, stateless philosophy

As explained in Python Workout, Second Edition

Functional programming is a programming paradigm that emphasizes composing computations as functions and minimizing mutable state and side effects.

Functional programming composes computations as functions, keeps functions short, and minimizes mutable state by treating data as immutable. Short functions have fewer paths to test and are easier to understand, debug, and maintain. section 8.0

The approach relies on several practical habits:

  • Pass functions as arguments to other functions. section 8.0
  • Transform sequences of inputs into sequences of outputs instead of modifying existing data. section 8.0
  • Combine functional techniques with object-oriented designs when each approach makes the code shorter and clearer. section 10.34

Replacing mutation with transformation

If an immutable Person needs a different name, create a new Person based on the old one rather than changing the existing object. In a Zoo class, generator expressions and sum can filter animals or total their legs without long procedural functions. section 8.0 section 10.34

How Python Workout, Second Edition covers it

How deep it goes
Explains the rationale for immutability and statelessness, then illustrates transformations with generator expressions and aggregation.
Examples
Uses an immutable Person replacement and Zoo queries that filter animals and sum their legs.
What it assumes
Assumes familiarity with Python functions and core data structures, while treating function passing and immutable transformations as practical techniques.
What you can do after
Apply functional techniques to write shorter functions with fewer state-dependent paths and easier testing.
How it connects
Connects immutable data, higher-order functions, generator expressions, and object-oriented programming.

Also explained in

Compare how 19 books explain functional, stateless philosophy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout, Second Edition. The section links point to the parts of the book it draws on.