Encapsulation in Python
Encapsulation is an essential aspect of object-oriented programming.
Let’s explain encapsulation in plain words: information hiding. This means delimiting the internal interface and attributing from the external world.
The benefits of information hiding are reducing system complexity and increasing robustness. Why? Because encapsulation limits the interdependencies of different software components.
Suppose we create a module. Our users could only interact with us through public APIs; they don’t care about the internals of this module. Even when the details of internal implementation change, the user’s code doesn’t need a corresponding change.
To implement encapsulation, we need to learn how to define and use a private attribute and a private function.
Enough theory now, let’s talk about how we do this in Python.
Python is an interpreted programming language and implements weak encapsulation. Weak encapsulation means it is performed by convention rather than being enforced by the language. So, there are some differences from Java or C++.
Protected Attribute and Method
If you have read some Python code, you will always find some attribute names with a prefixed underscore. Let’s write a simple
The output will be:
From the result, an attribute or method with a prefixed underscore acts the same as the normal one. So, why do we need to add a prefixed underscore for an attribute?
The prefix underscore is a warning for developers: please be careful about this attribute or method, don’t use it outside of the declared
pylint will report this kind of bad code smell:
Another benefit of the prefix score is that it avoids wildcard importing of the internal functions outside of the defined module. Let’s have a look at this code:
# foo module: foo.py
print("func_a called!")def _func_b():
Then, if we use a wildcard import in another part of the code:
from foo import *func_a()
We will encounter an error:
By the way, wildcard import is another bad smell in Python and we should avoid this in practice.
Private Attribute and Method
In traditional OOP languages, why can private attributes and methods not be accessed by a derived
Class? Because it is useful in information hiding.
Suppose we declare an attribute with the name
mood, but in the derived
Class, we re-declare another attribute with the name
mood. This overrides the previous one in the parent
Class and will likely introduce a bug in the code.
So, how do we use the private attribute in Python?
The answer is adding a double prefix underscore in an attribute or method. Let’s run this code snippet:
The output will be:
public value in Base
private value in Base
We call the
public function from a derived object, which will invoke the
public function in the
__private is a private method, only the object itself could use it, there is no naming conflict for a private method.
If we add another line of code:
It will trigger another error:
Why? Let’s print all the methods of the object and find out if there is a method with the name
_Base__private. Run the code snippet:
This is called
name mangling which the Python interpreter applies. Because the name had the added
Class prefix name, private methods are protected carefully from getting overridden in the derived class.
Again, this means we can use
d._Base__private to call the private function. Remember, it’s not enforced!
Thanks for reading.