Explore
Placement Prep

Edit

Edit

# Encapsulation In Python | FACE Prep

Published on 09 Mar 2020

Encapsulation in Python is the process of wrapping up variables and methods into a single entity.In programming, a class is an example that wraps all the variables and methods defined inside it.

In the real world, consider your college as an example. Each and every department's student records are restricted only within the respective department. CSE department faculty cannot access the ECE department student record and so on. Here, the department acts as the class and student records act like variables and methods.

Before understanding its working, let us look at the advantages of using Encapsulation in programs.

## Why Do We Need Encapsulation?

Encapsulation acts as a protective layer by ensuring that, access to wrapped data is not possible by any code defined outside the class in which the wrapped data are defined. Encapsulation provides security by hiding the data from the outside world.

In Python, Encapsulation can be achieved by declaring the data members of a class either as private or protected. In Python, 'Private' and 'Protected' are called Access Modifiers, as they modify the access of variables or methods defined in a class. Let us see how access modifiers help in achieving Encapsulation.

## Encapsulation Using Private Members

If we declare any variable or method as private, then they can be accessed only within the class in which they are defined. In Python, private members are preceded by two underscores. In the below example, 'length' and 'breadth' are the two private variables declared and can be accessed within the class 'Rectangle'.

class Rectangle:
__length = 0 #private variable
__breadth = 0#private variabledef __init__(self): #constructorself.__length = 5self.__breadth = 3#printing values of the private variable within the class
print(self.__length)

rec = Rectangle() #object created for the class 'Rectangle'
#printing values of the private variable outside the class using the object created for the class 'Rectangle'
print(rec.length)
Output:
5
3
Traceback (most recent call last):
File "main.py", line 11, in <module>
print(rec.__length)
AttributeError: 'Rectangle' object has no attribute '__length'


Since we have accessed private variables in the main() method, i.e., outside the class 'Rectangle', we got an error. Hence in the above program, Encapsulation is achieved using the private variables 'length' and 'breadth'. Next, let us see how to achieve Encapsulation using protected members.

## Encapsulation Using Protected Members

Protected members can be accessed within the class in which they are defined and also within the derived classes.In Python, protected members are preceded by a single underscore. In the below example, 'length' and 'breadth' are the two protected variables defined inside the class 'Shape'.

class Shape:#protected variables
_length = 10

class Circle(Shape):def __init__(self):#printing protected variables in the derived class
print(self._length)

cr = Circle()
#printing protected variablesoutsidethe class 'Shape' in which they are defined
print(cr.length)
Output:
10
20
Traceback (most recent call last):
File "main.py", line 11, in <module>
print(cr.length)
AttributeError: 'Circle' object has no attribute 'length'


When we try to access protected variables in the derived class, we got output. But, in the main() method, we got an error. Hence in the above example, Encapsulation is achieved using the protected variables 'length' and 'breadth'.

## Encapsulation In Python FAQs

1) What is Encapsulation in Python?

Encapsulation in Python is the process of wrapping up variables andmethods into a single entity. In programming, a class is an example that wraps all the variables and methods defined inside it.

2) How can we achieve Encapsulation in Python?

In Python, Encapsulation can be achieved using Private and Protected Access Members.

3) How can we define a variable as Private?

In Python, Private variables are preceded by using two underscores.

4) How can we define a variable as Protected?

In Python, Protected variables are preceded by using a single underscore.