Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. OOPs concept in Python
  5. Data hiding in Python

Data hiding in Python

Essential features of object oriented programming:-

  • Data hiding
  • Inheritance
  • Overriding
  • Polymorphism

Data hiding is the process of hiding the details of an object and function. It is also a potent technique in programming that results in data security and less data complexity.

In python, if we want to hide the variable, then we need to write double underscore (__) before the variable name.

Syntax:-

__variablename

e.g.,

class democlass: 
	__hiddenVariable = 0
	def add(self, increment): 
		self.__hiddenVariable += increment 
		print (self.__hiddenVariable) 
demoobject = democlass()	 
demoobject.add(2) 
demoobject.add(5) 
print (demoobject.__hiddenVariable) 
Output-
4
12
error

This is all about data hiding in python.

Was this article helpful to you? Yes No

How can we help?