8 Encapsulation

The first of the three main tenets of object-oriented programming is encapsulation. This word might conjure up an image of a space capsule, a cell wall, or a medicine gelcap, where the precious cargo inside is protected from the outside environment. In programming, encapsulation has a similar but even more detailed meaning: hiding internal details of state and behavior from any external code and having all code in one place.

In this chapter, we’ll see how encapsulation works with functions, then with methods of objects. I’ll discuss different interpretations of encapsulation: using direct access versus using getters and setters. ...

Get Object-Oriented Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.