Encapsulation in Python

How to reduce your system’s complexity

Coder’s Cat
Mar 18, 2020 · 4 min read
Image for post
Image for post
Image credit @dtopkin1 on unsplash.com

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 Class:

The output will be:

new name
called _protected_print

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 Class!

pylint will report this kind of bad code smell:

Image for post
Image for post

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
def func_a():
print("func_a called!")
def _func_b():
print("_func_b called!")

Then, if we use a wildcard import in another part of the code:

from foo import *func_a()

We will encounter an error:

Image for post
Image for post

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
derived protected

We call the public function from a derived object, which will invoke the public function in the Base class.

Note: Because __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:

Image for post
Image for post

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:

print dir(d)
Image for post
Image for post

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.

Better Programming

Advice for programmers.

Sign up for The Best of Better Programming

By Better Programming

A weekly newsletter sent every Friday with the best articles we published that week. Code tutorials, advice, career opportunities, and more! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Thanks to Zack Shapiro

Coder’s Cat

Written by

http://coderscat.com Write stuff about programming languages, algorithms, and architecture.

Better Programming

Advice for programmers.

Coder’s Cat

Written by

http://coderscat.com Write stuff about programming languages, algorithms, and architecture.

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store