Chapter 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

You’ve already seen that organizing your code into functions, classes, and modules is a great way to separate concerns, but you can also use these techniques to separate complexity in your code. Because it’s difficult to remember every detail about your software at all times, in this chapter you’ll learn to use abstraction and encapsulation to create levels of granularity in your code so you can worry about the details only when you need to.

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 runs 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”

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

3.6.1. Square pegs in round holes

3.6.2. Clever gets the cleaver

Summary

sitemap