beta

3 Abstraction and Encapsulation

This chapter covers:

  • Understanding the value of abstraction in large systems
  • Encapsulating related code into classes
  • Using encapsulation, inheritance, and composition in Python
  • Recognizing programming styles in Python

3.1  What is abstraction?

When you hear the word abstract, what do you think of? Usually a Jackson Pollock painting or a Calder sculpture run through my mind. Abstract art is marked by a freedom from concrete form, often only suggestive of a specific subject. Abstraction, then, would be the process of taking something concrete and stripping it of specifics. When speaking about abstraction in software, this is exactly right!

3.1.1  The "black box"

As you develop software, pieces of that software will come to represent a concept in full. Once you’ve finished developing a particular function, it can be used for its intended purpose over and over again without having to think too hard about how it works. At this moment, the function has become a black box. A black box is a calculation or behavior that "just works" — it doesn’t need to be opened up and examined each time you need it (shown in figure 3.1).

Figure 3.1. Treating working software as a black box
CH03 FIG01 CLaP

Suppose you’re building a natural-language processing system that determines if a product review is positive, negative, or neutral. Such a system has many steps along the way, as shown in figure 3.2:

3.1.2  Abstraction is like an onion

3.1.3  Abstraction is a simplifier

3.1.4  Decomposition enables abstraction

3.2  Encapsulation

3.2.1  Encapsulation constructs in Python

3.2.2  Expectations of privacy in Python

3.3  Try it out

3.3.1  Refactoring

3.4  Programming styles are an abstraction, too!

3.4.1  Procedural programming

3.4.2  Functional programming

3.4.3  Declarative programming

3.5  Typing, inheritance, and polymorphism

3.6  Recognizing the wrong abstraction

×

Ups!

Unfortunatly, we weren't able to load your books.
Retry

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage