Encapsulation is an essential aspect of Object-Oriented Programming.
Let’s explain encapsulation in plain words: information hiding. Encapsulation means delimiting the internal interface and attribute from the external world.
The benefit of information hiding is 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 internals implementation changed, the user’s code doesn’t need a corresponding change.
To implement encapsulation, we need to learn how to define and use 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 implies the programming language will not enforce it but only provide a convention. So there are some differences with 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 Class:
The output will be:
From the result, an attribute or method with a prefixed underscore acts the same as the normal one.
So, why 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 declared Class!
pylint will report out this kind of bad smell code:
Another benefit of prefix score is: it avoids wildcard importing the internal functions outside of the defined module. Let’s have a look at this code:
Then if we use wildcard import in another part of code:
We will encounter an error:
By the way, wildcard import is another bad smell in Python and we should avoid in practice.
Private attribute and method
In traditional OOP programming languages, why private attributes and methods can not accessed by derived Class?
Because it is useful in information hiding. Suppose we declare an attribute with name
mood, but in the derived Class we redeclare another attribute of name
mood. The second one overrides the previous one in the parent Class and will likely introduce a bug in code.
So, how to 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:
We call the
public function from a derived object, which will invoke the
public function in Base class. Note this, because
__private is a private method, only object its self could use it, there is no naming conflict for a private method.
If we add another line of code:
It will trigger another error:
Let’s print all the methods of object and find out there a method with name of
This is called
name mangling that the Python interpreter applies. Because the name was added
Class prefix name, private methods are protected carefully from getting overridden in derived Class.
Again, this means we can use
d._Base__private to call the private function. Remember, it’s not enforced!
Join my Email List for more insights, It's Free!😋