liveBook · Manning

Abstraction

As explained in Python Workout, Second Edition

Abstraction is the representation of functionality at a level that hides lower-level implementation details.

Abstraction represents functionality at a higher level by giving a useful name to a package of behavior while setting lower-level details aside. Functions reduce repetition and let programs be reasoned about in terms of meaningful actions rather than every operation involved. section 7.0

Use abstraction to make code easier to understand, change, and extend:

  • Wrap repeated behavior in a function and call it by name instead of duplicating its implementation. section 7.0
  • Define objects and classes around distinct responsibilities, allowing code to use a concise interface without depending on internal implementation. section 10.2
  • Preserve an interface while changing the implementation, such as revising how an average is calculated without changing how callers use it. section 10.2
  • Treat functions as data when useful: store them in data structures, retrieve them, and use them to reduce code and increase flexibility. section 7.0

A bookshelf as an abstraction

Saying “bookshelf” communicates a concept more briefly and powerfully than describing wooden boards, fasteners, orientation, and storage spaces. A class provides the same kind of shorthand for program concepts: users focus on its methods and behavior rather than its internal construction. section 10.2

How Python Workout, Second Edition covers it

How deep it goes
Explains the purpose and maintenance benefits of abstraction through functions and objects, but does not develop formal abstraction theory.
Examples
Uses repeated code, named programming actions, a bookshelf, and a replaceable average calculation to illustrate abstraction.
What you can do after
Apply abstraction by packaging repeated behavior in functions and separating object interfaces from changeable implementations.
How it connects
Connects abstraction with functions, objects, classes, interfaces, implementations, inheritance, and the DRY principle.

Also explained in

Compare how 15 books explain abstraction →

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.