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:
- It helps to prevent damage or misuse of volatile data by hiding it from the public.
- The class objects are disconnected from the irrelevant data.
- It isolates objects as the basic concept of OOP.
- It increases the security against hackers that are unable to access important data.
Disadvantages of Data Hiding:
- It enables programmers to write lengthy code to hide important data from common clients.
- The linkage between the visible and invisible data makes the objects work faster, but data hiding prevents this linkage.
Similar Reads
Inbuilt Data Structures in Python Python has four non-primitive inbuilt data structures namely Lists, Dictionary, Tuple and Set. These almost cover 80% of the our real world data structures. This article will cover the above mentioned topics. Above mentioned topics are divided into four sections below. Lists: Lists in Python are one
3 min read
Data Wrangling in Python Data Wrangling is the process of gathering, collecting, and transforming Raw data into another format for better understanding, decision-making, accessing, and analysis in less time. Data Wrangling is also known as Data Munging. Python Data WranglingImportance Of Data Wrangling Data Wrangling is a v
10 min read
Loading Different Data Files in Python We are given different Data files and our task is to load all of them using Python. In this article, we will discuss how to load different data files in Python. Loading Different Data Files in PythonBelow, are the example of Loading Different Data Files in Python: Loading Plain Text Files Loading Im
2 min read
How To Read .Data Files In Python? Unlocking the secrets of reading .data files in Python involves navigating through diverse structures. In this article, we will unravel the mysteries of reading .data files in Python through four distinct approaches. Understanding the structure of .data files is essential, as their format may vary w
4 min read
Interesting facts about Python Lists Python lists are one of the most powerful and flexible data structures. Their ability to store mixed data types, support dynamic resizing and provide advanced features like list comprehension makes them an essential tool for Python developers. However, understanding their quirks, like memory usage a
6 min read
Data Classes in Python | An Introduction dataclass module is introduced in Python 3.7 as a utility tool to make structured classes specially for storing data. These classes hold certain properties and functions to deal specifically with the data and its representation.DataClasses in widely used Python3.6Â Although the module was introduced
3 min read