liveBook · Manning

Hidden implementation

As explained in Software Design for Python Programmers

A hidden implementation is internal class code that is inaccessible to client code and may change without affecting permitted users of the class.

A hidden implementation keeps class internals inaccessible to client code, allowing the implementation to change without forcing changes in permitted users. In Python, leading underscores conventionally mark private state and helper methods. section 5.1

Public interface versus hidden detail

Client code should use a class's required public operations, not read `_x` or call `_calculate_pay()`. The class can later replace either hidden detail while preserving its public behavior. section 5.1

Apply the technique by following these rules:

  • Make instance variables and methods private unless they must be public. section 5.1
  • Use properties for controlled access without exposing state representation; reject invalid state and avoid setters for immutable objects. section 5.9
  • Minimize dependencies between classes, following the Principle of Least Knowledge, also called the Law of Demeter. section 5.1 section 5.9

How Software Design for Python Programmers covers it

How deep it goes
Defines Python's privacy convention and extends it to properties, immutable state, refactoring, and interclass dependency rules.
Examples
Uses `_x` and `_calculate_pay()` to illustrate implementation details that client code should not reference.
What it assumes
Assumes familiarity with Python classes, instance members, methods, and client code; treats privacy as a programmer-respected convention.
What you can do after
Apply it via Python underscore conventions and properties to refactor class internals without breaking permitted client code.
How it connects
Connects encapsulation with the Principle of Least Knowledge, Law of Demeter, properties, immutability, and the Open-Closed Principle.

Related concepts

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