Data Hiding

An object's attributes may or may not be visible outside the class definition. You need to name attributes with a double underscore prefix, and those attributes then are not be directly visible to outsiders.

Program on data hiding

class abc:
__a=10  # private member
b=20   #public member
def put(self):
print(self.__a)

x=abc()
x.put()
print(x.b)
#print(x.__a) not accessable

Program on private methods and variables
class abc:
__a=10
b=20
def put(self):
print(self.__a)
def __put2(self):
print("private method",self.b)
def put1(self):
__x=30
print("Public method put1",__x)
self.__put2()
x=abc()
x.put()
print(x.b)
x.put1()
#print(x.__a) not accessable

 

 

 

For
Online Classes

Contact Us: 9885348743