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
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!
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
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: