Menu Close

Data hiding in Python

In this article we are going to learn the most important part of object oriented program is data hiding.

To understand this example you should have basic knowledge of python programming. To learn basic Python click here.

Before learn Data hiding in python You should have basic knowledge of Public, Private and Protected specifiers in Python. You can learn from Python access modifiers.

What is Data hiding in Python

Data hiding in Python is one of the most important features of object oriented programming In Python. Data hiding provide a facility to prevent the methods and attributes of a class to access 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 member.

Public

The member of class are define public which 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 declared as private which are accessible by inside class only. It is not accessible outside of the class even any child class. To create private data inside the class,

To create private data members inside the class, we use double underscore (__) before the data member.

Protected

The members of a class declared as protected which are not accessible by 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 protected data member inside class, we use 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 class form outside of the class for example ( Person.__username and Person.__password), then it threw an error.

To access private data member of class we use 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 give 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 constrol the access of the attributes and methods in the Python class.
To understand this 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

Previous Article
Next Article

Was this post helpful?

Related Posts