-
Book Overview & Buying
-
Table Of Contents
Python Illustrated
By :
So, what is encapsulation? If you think about the meaning, it’s pretty much that something is wrapped around something. This is the case in coding as well. Encapsulation is the idea of hiding internal state (the value of the attributes) and requiring all interaction to be performed through an object’s methods.
This is not just a Python concept; it’s a universal concept. For Python, it works a little funky because true privacy is not strictly enforced in Python. Instead, we work with naming conventions we can follow to tell everyone who uses our code: “This is internal, please don’t touch directly.”

Here’s how the naming conventions work.
By default, everything is public. We can use a leading underscore (_) to indicate that a variable or method is private. For example, _secret_attribute. A double underscore (__) triggers some internal magic (more on...