Encapsulation in Python

Encapsulation in Python

Encapsulation is one of the basic concepts in Object-Oriented Programming. It deals with wrapping data and the methods in a Class by restricting other Classes from accessing its data and methods. It is also known as hiding the information in simple terms. We can achieve encapsulation in Python through the ways listed below.

  • Private Members
  • Protected Members
  • Getters and Setters

All the three ways mentioned above restrict access in one way or the other and let us find out how.

Private members in Python

#Python program to show the implementation of Private members in a class
class Student:
  #constructor method
  def __init__(self,name,marks):
    self.name = name
    self.__marks = marks #private member

#object of class
stu = Student("Anoop",2000)
print("Student's marks: {}".format(stu.__marks))
Output:

AttributeError: 'Student' object has no attribute '__marks'

In the program above, we added a private variable called marks by prefixing it with double underscores. PVM prevented us from accessing this variable through the object ‘stu’ by displaying an error in the output.

We should follow name mangling to access private and protected members from a class. Please find below the format of name mangling.

#FORMAT OF THE NAME MANGLING
_nameofclass__datameber
#one underscore before class name and two underscores before data member’s name.
#Python program to show name mangling concept
class Student:
  #constructor method
  def __init__(self,name,marks):
    self.name = name
    self.__marks = marks

#creating object of class
stu = Student('Anoop',2000)
print("Name of student: ",stu.name) #accessing public data member
print("Marks of student: ",stu._Student__marks)
Output:

Name is:  Anoop
Marks are:  2000

You can notice that we accessed the private member outside the class by prefixing one underscore before the class name and two underscores before the data member’s name. You can also access the same private member by declaring a public method within the class, as shown in the program below.

#Python program to show how we can use public methods
#to access private data members
class Student:
  #constructor method
  def __init__(self,name,marks):
    self.name = name
    self.__marks = marks
  
  def show(self):
    print("Name is: ",self.name)
    print("Marks are: ",self.__marks)

#creating object of class
stu = Student('Anoop',2000)
stu.show()
Output:

Name is:  Anoop
Marks are:  2000

Protected Members in Python

Protected members are the data members accessible within the class and its subclasses. We define a protected member using only one underscore.

#Python program to show protected data members
#Parent class
class School:
  def __init__(self):
    self._qualification = "Graduate"

#Child Class
class Teacher(School):
  def __init__(self,fullname):
    self.fullname = fullname
    School.__init__(self)
  
  def show(self):
    print("Teacher's name is: ",self.fullname)
    print("Teacher's qualification is: ",self._qualification)

teacher1 = Teacher("Suriya")
teacher1.show()

#Trying to access protected member directly
print("Qualification once again: ",teacher1._qualification)
Output:

Teacher's name is:  Suriya
Teacher's qualification is:  Graduate
Qualification once again:  Graduate

You can notice in the program above that we declared a protected data member called _qualification. We tried accessing this data member in the show() method and directly using the object teacher1.

Getters and Setters

If you want to implement encapsulation properly, we need to use the getters and setters in Python. The main objective of using getters and setters is to ensure the data is encapsulated appropriately in a program. We use the getter methods to access data members and setter methods to update the data members.

#Python program to show implementation of
#Getters and Setters
class Employee:
  def __init__(self,ename,eno):
    self.ename = ename
    self.__eno = eno #private member
  
  #getter method
  def get_eno(self):
    return self.__eno
  
  #setter method
  def set_eno(self,eno):
    self.__eno = eno

emp = Employee('Nikhil',25)

#accessing eno using getter
print("Name of employee is: ",emp.ename)
print("Employee number is: ",emp.get_eno())

emp.set_eno(30)
print("Updated number of employee is: ",emp.get_eno())
Output:

Name of employee is:  Nikhil
Employee number is:  25
Updated number of employee is:  30

In the program above, get_eno() is the getter method that returns the value, and set_eno() is the setter method used for updating the value.

Scroll to Top