Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 functions 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)
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 using name mangling ?
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)
5 15 15
Example 3 - Private and Protected Members
Let's look at another example of data hiding 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)
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 Employee class.
Name Mangling
Python performs name mangling for private attributes. When you declare __variablename, Python internally changes it to _Classname__variablename. This is why private variables can still be accessed using the mangled name.
Advantages of Data Hiding
| Advantage | Description |
|---|---|
| Enhanced Security | Encapsulates important data from external access |
| Reduced Complexity | Hides implementation details from end users |
| Data Integrity | Prevents creation of links to wrong data |
Conclusion
Data hiding in Python is achieved using double underscore (__) prefix for private members. While Python doesn't enforce true privacy, name mangling provides a level of encapsulation that helps maintain code structure and security.
