Data Hiding in Python
What is Data Hiding?
Data hiding is a part of object-oriented programming, which is generally used to hide the data information from the user. It includes internal object details such as data members, internal working. It maintained the data integrity and restricted access to the class member. The main working of data hiding is that it combines the data and functions into a single unit to conceal data within a class. We cannot directly access the data from outside the class.
This process is also known as the data encapsulation. It is done by hiding the working information to user. In the process, we declare class members as private so that no other class can access these data members. It is accessible only within the class.
Data Hiding in Python
Python is the most popular programming language as it applies in every technical domain and has a straightforward syntax and vast libraries. In the official Python documentation, Data hiding isolates the client from a part of program implementation. Some of the essential members must be hidden from the user. Programs or modules only reflected how we could use them, but users cannot be familiar with how the application works. Thus it provides security and avoiding dependency as well.
We can perform data hiding in Python using the __ double underscore before prefix. This makes the class members private and inaccessible to the other classes.
Let's understand the following example.
1 2 Traceback (most recent call last): File "<string>", line 17, in <module> AttributeError: 'CounterClass' object has no attribute '__privateCount'
However we can access the private member using the class name.
1 2 2
Advantages of Data Hiding
Below are the main advantages of the data hiding.
Disadvantages of Data Hiding
Every coin has two sides if there are advantages then there will be disadvantage as well. Here are the some disadvantages are given below.
Data hiding is an important aspect when it comes to privacy and security to particularly within the application. It plays an essential role in preventing unauthorized access. It has some disadvantages, but these are avoidable in front of its advantages.