Learn Python Programming

Python – Encapsulation


Encapsulation refers to wrapping up of data under a single unit. In other words, it is the mechanism that binds code and the date that it manipulates. It is a protected shield that prevents the data being accessed from code outside this shield and in this the variables and the data of a class is hidden from any other class can be accessed only through any member function of the own class in which they are declared.

We can relate to a medical capsule where the drug is always safe inside the capsule, similarly using encapsulation, the methods and variables of the class are very well hidden and safe from outside code.

What are the advantages of using Encapsulation?

There are several advantages of using encapsulation.

  • Data hiding
  • Flexibility
  • Reusability

Data hiding: It means a user will not have any ideas about the inner implementation of the class and even the user will not be able to know of how the class is storing values in the variables. A user can only see the passing the values to a method and then variables are getting initialized with that values.

Flexibility & Reusability: The increased flexibility offers control over the variables and methods to how to use them either read only a method or write only depending on what we require. It provides the reusability and easy to change with new requirements.

Difference between Abstraction and Encapsulation

Encapsulation is know for wrapping the code and data for the necessary information whereas abstraction basically provides the abstract or gives you the most necessary details in the data and it is only the useful details that you will be seeing in abstraction. But incase of encapsulation the whole code and all the necessary information are wrapped together. In encapsulation you only focus on how the implementation will work on any given methods or variables.

How encapsulation work in Python?

In python, plain attributes are used to achieve the encapsulation. It is done with the help of naming convention through which we are able to distinguish between protected and private members.

In python, private methods use the double underscore naming convention.

class Sharequery():
    def __init__(self):
        self.course = "Python Programming Language"
        self.__tech   =  "python"

    def courseName(self):
        return self.course + self.__tech

obj = Sharequery()

print(obj.course)
print(obj.__tech)
print(obj.courseName())

#OUTPUT
Python Programming Language
Traceback (most recent call last):
  File "encapsulation.py", line 12, in <module>
    print(obj.__tech)
AttributeError: 'Sharequery' object has no attribute '__tech'

In the above example, you will get an attribute error which is saying the Sharequery object has no attribute. But you have written __tech attribute. So while trying to access the variable out of scope of class, you are not actually getting the any values.

Here by using name mangling technique, you can access private class members.

class Sharequery():
    def __init__(self):
        self.course = "Python Programming Language"
        self.__tech   =  "python"

    def courseName(self):
        return self.course + self.__tech

obj = Sharequery()

print(obj.course)
print(obj._Sharequery__tech)
print(obj.courseName())

#OUTPUT
Python Programming Language
python
Python Programming Languagepython