Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

4/5/2021 How to use Encapsulation in Python - Python

Python GUI PyQT Machine Learning Web


Learn Python Programming

How to use Encapsulation in Python


An objects variables should not always be directly accessible.

To prevent accidental change, an objects variables can sometimes only be changed with an
objects methods. Those type of variables are private variables.

The methods can ensure the correct values are set. If an incorrect value is set, the method
can return an error.

Related course: Python Programming Courses & Exercises

Encapsulation example
Python does not have the private keyword, unlike some other object oriented languages,
but encapsulation can be done.

Instead, it relies on the convention: a class variable that should not directly be accessed
should be prefixed with an underscore.

PYTHON

class Robot(object):
def __init__(self):
self.a = 123
self._b = 123
self.__c = 123

obj = Robot()
print(obj.a)
print(obj._b)
print(obj.__c)

If you run the program you see:


https://pythonprogramminglanguage.com/encapsulation/ 1/4
4/5/2021 How to use Encapsulation in Python - Python

PYTHON

123
123
Traceback (most recent call last):
File "test.py", line 10, in <module>
print(obj.__c)
AttributeError: 'Robot' object has no attribute '__c'

So what’s with the underscores and error?

A single underscore: Private variable, it should not be accessed directly. But nothing stops
you from doing that (except convention).

A double underscore: Private variable, harder to access but still possible.

Both are still accessible: Python has private variables by convention.

Getters and setters

Private variables are intended to be changed using getter and setter methods. These
provide indirect access to them:

PYTHON

class Robot(object):
def __init__(self):
self.__version = 22

def getVersion(self):
print(self.__version)

def setVersion(self, version):


self.__version = version

obj = Robot()
obj.getVersion()
obj.setVersion(23)
obj.getVersion()
print(obj.__version)

This then outputs the variables values:

PYTHON

https://pythonprogramminglanguage.com/encapsulation/ 2/4
4/5/2021 How to use Encapsulation in Python - Python

22
23

The class with private attribute and methods.

The values are changed within the class methods. You could do additional checks, like if
the value is not negative or to large.

If you are a Python beginner, then I highly recommend this book.

Download exercises

Back Next Post

Leave a Reply:

Email address

Message

I'm not a robot


reCAPTCHA
Privacy - Terms

Send Message

Paritosh Parmar • Sun, 03 May 2020


https://pythonprogramminglanguage.com/encapsulation/ 3/4
4/5/2021 How to use Encapsulation in Python - Python

This code should print 3 values.

obj = Robot()
obj.getVersion()
obj.setVersion(23)
obj.getVersion()
print(obj.__version)

Isn't it?

dev • Tue, 05 May 2020

Just two, the last line isn't allowed as it's a private variable of the class Robot. It
has two underscores which doesn't allow the variable to be accessed outside the
class.

You'd see this error:

AttributeError: 'Robot' object has no attribute '_version'

However you would see 3 values printed if you remove all the underscores.

Cookie policy | Privacy policy | Contact | Zen | Get

© 2021 https://pythonprogramminglanguage.com

https://pythonprogramminglanguage.com/encapsulation/ 4/4

You might also like