In this article, we are going to learn the most important part of an object-oriented program called data hiding. To understand this concept you should have basic knowledge of Python programming. You can check out our Python tutorial to brush up your skills.
Before learning Data hiding in Python You should have basic knowledge of Public, Private, and Protected specifiers in Python. You can learn from the Python access modifiers tutorial.
Headings of Contents
What is Data hiding in Python?
Data hiding in Python is one of the most important features. Data hiding in Python allows us to prevent the access of methods and attributes of a class directly.
By default all the members of the class access outside of the class but sometimes we want to prevent them, Then we can use data-hiding concepts.
We can prevent this by making private and protected data members.
Public
The members of the class are defined as public and are easily accessible from any part of the program but within a program. In class all the member and member function by default is public.
Private
The members of the class are declared as private which is accessible by inside class only. It is not accessible outside of the class even in any child class. To create private data inside the class,
To create private data members inside the class, we have to use a double underscore (__) before the data member.
Protected
The members of a class declared as protected are not accessible outside of the class, however, it is accessible only by inherited class or child class.
To create protected data inside the class, we use a single underscore (_) before the data member.
To create a protected data member inside the class, we use a single underscore (_) before the data member.
Data hiding examples
Here will take some examples to understand data hiding in Python.
Example 1:
class Person:
# hidden members of the class - Private member
__username = 'programmingfunda'
__password = 'python121'
def Access(self):
print("Usename is:- {}".format(self.__username))
print("password is:- {}".format(self.__password))
p1 = Person()
p1.Access()
Output
Usename is:- programmingfunda
password is:- python121
If you try to access a private member of the class form outside of the class for example ( Person.__username and Person.__password), then it will throw an error.
To access private data members of the class we have to use a tricky syntax:- objectName._className__attrName for example ( p1._Person__username)
Example 2:
class Person:
# hidden members of the class - Private member
__username = 'programmingfunda'
__password = 'python121'
def Access(self):
print("Usename is:- {}".format(self.__username))
print("password is:- {}".format(self.__password))
p1 = Person()
p1.Access()
print("-------------------------")
print(p1._Person__username)
print(p1._Person__password)
Output
Usename is:- programmingfunda
password is:- python121
-------------------------
programmingfunda
python121
Private and Protected data member
class Person:
# hidden members of the class
__username = 'programmingfunda' #Private member
_password = 'python121' #Protected member
def Access(self):
print("Usename is:- {}".format(self.__username))
print("password is:- {}".format(self._password))
p1 = Person()
p1.Access()
print("----------------------")
print(p1._Person__username)
Output
Usename is:- programmingfunda
password is:- python121
----------------------
programmingfunda
Printing Objects
Printing objects gives us information about objects we are working with by using __repr__ and __str__ methods.
Example
class myData:
def __init__(self, a, b):
self.a = a
self.b = b
def __repr__(self):
return f"value of a and b is {self.a} and {self.b}"
def __str__(self):
return f"value of a and b is {self.a} and {self.a}"
a1 = myData(12, 34)
print(a1) #Call __str__()
print(a1.__repr__()) #call __repr__()
Output
value of a and b is 12 and 34
value of a and b is 12 and 34
If __str__ is not defined then it will call __repr__() method automatically.
Example
class myData:
def __init__(self, a, b):
self.a = a
self.b = b
def __repr__(self):
return f"value of a and b is {self.a} and {self.b}"
a1 = myData(12, 34)
print(a1)
Output
value of a and b is 12 and 34
Conclusion
In this data hiding in Python article, you have learned all about data hiding in Python along with examples. This is the best way to constrain the access of the attributes and methods in the Python class.
To understand these examples you should have basic knowledge of Python access modifiers in Python.
If you like this data hiding in Python article, please share and keep visiting for further Python tutorials.
Python OOPs Concepts
For More Information:- Click Here