liveBook · Manning

Flexibility

As explained in Practices of the Python Pro

Flexibility is the capacity of software to be adapted to related or similar tasks.

Flexibility is software’s capacity to adapt to related tasks and changing requirements. Flexible code resists change less: implementations can be swapped, behaviors can be extended, and modifications stay localized. Designing for flexibility involves trade-offs, so improvements should serve realistic future needs rather than add complexity without purpose. section 1.4.1 section 7.1.2

Recognize flexibility through these practical signals:

  • Separate responsibilities so code can be generalized for several use cases or extended with more varied behavior. section 6.2.1
  • Prefer adding new behavior without disturbing existing code, while accepting that real systems sometimes require careful modifications. section 7.1.1
  • Reduce resistance before making a change through decomposition, encapsulation, and small continuous refactorings. section 7.1.2
  • Avoid premature abstractions: temporary duplication can be safer than an abstraction based on incomplete knowledge of future uses. section 7.1.1

Replace rigid conditionals with a mapping

A long chain of if/elif statements repeats the structure of each choice and makes changes harder to locate. A dictionary centralizes the mapping: choices = {'A': 'apples', 'B': 'bats', ...}; print(f'{choice} is for {choices[choice]}'). Adding or changing an outcome then focuses on the mapping instead of another conditional branch. section 7.1.2

Figure 10.7. How core business logic supports a variety of use cases

Use the diagram to relate reusable core business logic to multiple use cases, such as a command-line interface and a mobile application. section 10.4

How Practices of the Python Pro covers it

How deep it goes
Defines resistance to change and demonstrates decomposition, refactoring, mappings, and extension, without descending into implementation internals.
Examples
Demonstrates conditional-to-dictionary refactoring, feature addition, and reuse of core logic across command-line and mobile interfaces.
What it assumes
Assumes basic programming and code-reading ability while teaching flexibility through simple Python mappings and approachable design practices.
What you can do after
Apply it by refactoring rigid conditionals, separating responsibilities, and preparing existing code for localized changes or new behaviors.
How it connects
Connects flexibility with loose coupling, extensibility, separation of concerns, decomposition, encapsulation, inversion of control, and interfaces.

Also explained in

Compare how 9 books explain flexibility →

Related concepts

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