encapsulation in python
Python hosting: Host, run, and code Python in the cloud!
In an object oriented python program, you can restrict access to methods and variables. This can prevent the data from being modified by accident and is known as encapsulation. Let’s start with an example.
Related Courses:
Python Programming Bootcamp: Go from zero to hero
Private methods

We create a class Car which has two methods: drive() and updateSoftware(). When a car object is created, it will call the private methods __updateSoftware().
This function cannot be called on the object directly, only from within the class.
#!/usr/bin/env python |
This program will output:
updating software |
Encapsulation prevents from accessing accidentally, but not intentionally.
The private attributes and methods are not really hidden, they’re renamed adding _Car” in the beginning of their name.
The method can actually be called using redcar._Car__updateSoftware()
Private variables

Variables can be private which can be useful on many occasions. A private variable can only be changed within a class method and not outside of the class.
Objects can hold crucial data for your application and you do not want that data to be changeable from anywhere in the code.
An example:
#!/usr/bin/env python |
If you want to change the value of a private variable, a setter method is used. This is simply a method that sets the value of a private variable.
#!/usr/bin/env python |
Why would you create them? Because some of the private values you may want to change after creation of the object while others may not need to be changed at all.
Python Encapsulation
To summarize, in Python there are:
| Type | Description |
|---|---|
| public methods | Accessible from anywhere |
| private methods | Accessible only in their own class. starts with two underscores |
| public variables | Accessible from anywhere |
| private variables | Accesible only in their own class or by a method if defined. starts with two underscores |
Leave a Reply:
"If you want to change the value of a private value, an setter method is used. This is simply a method that sets the value of a private value."
there is a typo --> change the value of a private value --> change the value of a private variable
Thanks Tomislav! I updated it
so __ before a method or variable is considered as a private members is it...?
Yes, if you put __ before a method or variable it is a private member. This means you can only access it using a method.
It's weird that for python the default access for a variable is public...
Hi! It's cultural, in both Python and Java nothing prevents you from declaring all variables public, encapsulation is not strictly enforced in either language. In Java variables are private by default, in Python private variables are not enforced. In practice there is not a lot of difference because the developer has the authority on the code.
And in C++ a struct is a class where all the members are public by default.
Does Python support namespaces?
Hi Terry,
Yes there are namespaces, but there are a variety of ways to implement them.
I would recommend the third method because it's the most clean solution.
Method 1: Use dictionary
Everything is an object in Python, a dictionary can act as a namespace.
Method 2: Use packages
Create a directory /example/demo/ for your package. Inside it create a file called __init__.py with this contents:
Method 3: Use pkgutil
If you use pkgutil, you can create nested packages. Create a directory using this structure
<ul>
<li>/example/hello/</li>
<li>/example/hello/__init__.py</li>
<li>/example/hello/english/</li>
<li>/example/hello/english/__init__.py</li>
<li>/example/hello/dutch/</li>
<li>/example/hello/dutch/__init__.py</li>
<li>/example/test.py</li>
</ul>
In /example/hello/__init__.py put:
In /example/hello/english/__init__.py put:
In /example/hello/dutch/__init__.py put:
Your package is now complete. You can use nested namespaces.
Add this to /example/test.py and execute it:
first of all __maxspeed is private but i'm still able to access it with object name & another thing when i used print(redcar.__maxspeed) it prints updated value 10 could you please explain the behaviour
class Person:
__name #variable
class Programmer(Person)
self_Person__name ------> meaning full explanation please for this
Hi Amit,
The class Programmer inherits from the class Person. The Person class has a variable called name. Because Programmer inherits from Person, it has access to the name variable. For more on inheritance, read here.
Hi,Frank
What's the text font in the source code?
Hi Jacky, I think it's monospace
Hi Frank,
i am getting problem in file handling. i have written program which count number of lines and total words in file but it is not giving me correctly the number of words. my txt file is :
this program is giving me 58 words while there are not 58 words. Secondly when i write filename.read instead of filename.readlines then it does not give me correct number of lines. why?
If you print word inside the for word loop, it will show it splits the line into single characters. To split into words use this:
read() and readlines() are different methods. read() takes x characters from a file. readlines() reads the whole file and splits by newlines.
Many thanks :)
"The private method __updateSoftware() can only be called within the class itself. It can never be called from outside the class."
Why don't you tell that the method can actually be called using redcar._Car__updateSoftware() ?
The "private" attributes and methods are not really hidden, they're just automatically renamed adding "_Car" in the beginning of their name... This truly prevents from accessing them *accidentally* but not intentionally...
Thanks! I updated the post.