You are currently viewing Encapsulation In Python 3.8 ?
Encapsulation in python

Encapsulation In Python 3.8 ?

Encapsulation in python  is one of the most important and fundamental concerto to understand Object-Oriented-Programming (OOPS). It describes the ideal of building method in class. Here we put or hide some methods or variables in class. Some of method or variables can easily be access inside and outside the class but some variables or method can’t access outside the class. These are called private methods or variables.

Why we Need To Use Encapsulation?

Encapsulation is used to protect your data from the other users. They can’t access the code which you define in your class. Encapsulation provides the security by hiding the data from the outside world.

Here I clear Encapsulation with simple and easy example support a school management has student record in the office. Some part of record fee collection, admission information and accounts only accessible for specific staff in the school but student or outside the people can’t access that record.

In above example school is the class, record is the object or variable and students are the users.

In Java and C++ there are 4 types of variables used for example Public, no modifies, protected and private but In Python mostly variables are used in python class or method.

  1. Public
  2. Convention
  3. Private

Public Variables:

Public variable are those variable which user can easily access and modify the value of your code. In python public variable are indicate with (.) and with (_) underscore.


In Object-Oriented-Programming (OOP) A class is the container which include object, methods, arguments and variables.


#Encapsulation in python 
# A Sample class with init method
class student:

Here student is a class object. It contain all the detail about a student i.e. name, dob, enrollment date, father name, roll no., fee and other detail.

 You must read my previous tutorial how to use __int__ method in python. 

Example 01: Creating Class And Object In Python:

# A Sample class with init method
class student:

# init method or constructor, argument, attributes
def __init__(self, course, DOB,fee):
self.course = course
self.dob = DOB

Assign The value to the Arguments:

In this step we assign the value to the argument.

sadaqat = student('SCIENCE', '2020-11-01', 50)
raza = student('ARTS', '2020-10-06', 45)
ali = student('COMPUTER', '2020-09-04',40)

In above example I pick three student record which have different value of data. Now we access above data. And above three argument are public. Now just use the print method to get the student data.

print('SADAQAT COURSE IS :', sadaqat.course)
print('SADAQAT DOB IS :', sadaqat.dob)
print('SADAQAT DOB IS :', sadaqat.fee)

The Output Is:
SADAQAT DOB IS : 2020-11-01


Private Method:

Here i describe the private method in python with above simple example. And in the we change the one variable as private and will remain the same.

Example Of Private Method In Python:

Change the fee variable with double underscore (__) as below and run again your file. you will see the error to access the 2nd two argument.

#Encapsulation in python 
def __init__(self, course, DOB,fee): self.course = course self._dob = DOB self.__fee=fee Outpur is: G:\Project\sharafat\oops\venv\Scripts\python.exe G:/Project/sharafat/oops/ ================================== ------------OutPut--------------- SADAQAT COURSE IS : SCIENCE Traceback (most recent call last): File "G:/Project/sharafat/oops/", line 18, in print('SADAQAT DOB IS :', sadaqat.dob) AttributeError: 'student' object has no attribute 'dob'
Encapsulation in python

Traceback (most recent call last): File “G:/Project/sharafat/oops/”, line 18, in <module> print(‘SADAQAT DOB IS :’, sadaqat.dob) AttributeError: ‘student’ object has no attribute ‘dob’

In above example we try to access the all three variable but in out only one is success to access but 2nd and 3rd can’t access. Python give me the error in output because 2nd and 3rd are private. The private variable only be access inside the method.

Example 02 :Data Encapsulation In Python:

#Encapsulation in python 
class course:
def __init__(self):
self.__maxfee = 1000
def fee(self):
print("Course Max Fee Is: {}".format(self.__maxfee))
def setMaxFee(self, fee):
self.__maxfee = fee
c = course()
# change the price
c.__maxprice = 1100
c.fee()# using setter function