Encapsulation In Python | FACE Prep

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 variable
    def __init__(self): #constructor
        self.__length = 5
        self.__breadth = 3
        #printing values of the private variable within the class
        print(self.__length)
        print(self.__breadth)

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)
print(rec.breadth)
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
    _breadth = 20 

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

cr = Circle()
#printing protected variables outside the class 'Shape' in which they are defined
print(cr.length) 
print(cr.breadth)
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

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 Python, Encapsulation can be achieved using Private and Protected Access Members. 

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

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