Data Hiding in Python


Data hiding is also known as data encapsulation and it is the process of hiding the implementation of specific parts of the application from the user. Data hiding combines members of class thereby restricting direct access to the members of the class.

Data hiding plays a major role in making an application secure and more robust

Data hiding in Python

Data hiding in python is a technique of preventing methods and variables of a class from being accessed directly outside of the class in which the methods and variables are initialized. Data hiding of essential member function prevents the end user from viewing the implementation of the program hence increasing security. The use of data hiding also helps in reducing the complexity of the program by reducing interdependencies.

Data hiding in python can be achieved by declaring class members as private by putting a double underscore (__) as prefix before the member name.

Syntax

The syntax for hiding data in python is as follows −

__variablename

Example 1 - without class name

In this example, data hiding is performed by declaring the variable in the class as private

class hidden: # declaring private member of class __hiddenVar = 0 def sum(self, counter): self.__hiddenVar += counter print (self.__hiddenVar) hiddenobj = hidden() hiddenobj.sum(5) hiddenobj.sum(10) # print statement throws error as __hiddenVar is private print(hiddenobj.__hiddenVar)

Output

The output of the above code is as follows −

5
15
Traceback (most recent call last):
  File "main.py", line 12, in <module>
    print(hiddenobj.__hiddenVar)
AttributeError: 'hidden' object has no attribute '__hiddenVar'

Example 2 - with class name

In the following example, the hidden data can be accessed directly outside the class −

class hidden: # declaring hiddenVar private by using __ __hiddenVar = 0 def sum(self, counter): self.__hiddenVar += counter print (self.__hiddenVar) hiddenobj = hidden() hiddenobj.sum(5) hiddenobj.sum(10) # adding class name before variable to access the variable outside the class print(hiddenobj._hidden__hiddenVar)

Output

The output of the above code is as follows 

5
15
15

Example 3

Let’s look at another example of data hiding by using both private and protected members of the class

class Employee: # Hidden members of the class __password = 'private12345' # Private member _id = '12345' # Protected member def Details(self): print("ID: ",(self._id)) print("Password: ",(self.__password)+"\n") hidden = Employee() hidden.Details() print(hidden._Employee__password)

Output

The output of the above code is −

ID: 12345
Password: private12345
private12345

In the above output the Details function is part of Employee class, hence it can access both the private and protected members of the class. That is why id and password can be accessed without use of class name. However in the final print statement class name is required to access password since scope of private members is restricted within the class the Employee class.

Advantages of data hiding

  • Enhances security by encapsulating important data.
  • Hides irrelevant information from end user by disconnecting objects within class from useless data
  • Prevents creation of links to wrong data.

Updated on: 18-Aug-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements