Open In App

Data Hiding in Python

Last Updated : 28 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss data hiding in Python, starting from data hiding in general to data hiding in Python, along with the advantages and disadvantages of using data hiding in python.

What is Data Hiding?

Data hiding is a concept which underlines the hiding of data or information from the user. It is one of the key aspects of Object-Oriented programming strategies. It includes object details such as data members, internal work. Data hiding excludes full data entry to class members and defends object integrity by preventing unintended changes. Data hiding also minimizes system complexity for increase robustness by limiting interdependencies between software requirements. Data hiding is also known as information hiding. In class, if we declare the data members as private so that no other class can access the data members, then it is a process of hiding data.

Data Hiding in Python:

The Python document introduces Data Hiding as isolating the user from a part of program implementation. Some objects in the module are kept internal, unseen, and unreachable to the user. Modules in the program are easy enough to understand how to use the application, but the client cannot know how the application functions. Thus, data hiding imparts security, along with discarding dependency. Data hiding in Python is the technique to defend access to specific users in the application. Python is applied in every technical area and has a user-friendly syntax and vast libraries. Data hiding in Python is performed using the __ double underscore before done prefix. This makes the class members non-public and isolated from the other classes.

Example:

Python3




class Solution:
    __privateCounter = 0
 
    def sum(self):
        self.__privateCounter += 1
        print(self.__privateCounter)
 
 
count = Solution()
count.sum()
count.sum()
 
# Here it will show error because it unable
# to access private member
print(count.__privateCount)


Output:

Traceback (most recent call last):
  File "/home/db01b918da68a3747044d44675be8872.py", line 11, in <module>
    print(count.__privateCount) 
AttributeError: 'Solution' object has no attribute '__privateCount'

To rectify the error, we can access the private member through the class name :

Python3




class Solution:
    __privateCounter = 0
 
    def sum(self):
        self.__privateCounter += 1
        print(self.__privateCounter)
 
 
count = Solution()
count.sum()
count.sum()
 
# Here we have accessed the private data
# member through class name.
print(count._Solution__privateCounter)


Output:

1
2
2

Advantages of Data Hiding:

  1. It helps to prevent damage or misuse of volatile data by hiding it from the public.
  2. The class objects are disconnected from the irrelevant data.
  3. It isolates objects as the basic concept of OOP.
  4. It increases the security against hackers that are unable to access important data.

Disadvantages of Data Hiding:

  1. It enables programmers to write lengthy code to hide important data from common clients.
  2. The linkage between the visible and invisible data makes the objects work faster, but data hiding prevents this linkage.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads