Instance variables are those variables which depends upon the object.
They are not class specific, they are actually object specific.
We can also hide any instance variable.
Script: Write an script in Python which demonstrate how we can access hidden instance variable inside or outside the class.
class A:
def __init__(self):
self.__x=10
self.__y=20
def printresult(self):
print("The value of y is",self._A__y) #Accessing inside the class
a1=A()
print(a1._A__x) #accessing outside the class
a1.printresult()
Output:
10
The value of y is 20
They are not class specific, they are actually object specific.
We can also hide any instance variable.
Script: Write an script in Python which demonstrate how we can access hidden instance variable inside or outside the class.
class A:
def __init__(self):
self.__x=10
self.__y=20
def printresult(self):
print("The value of y is",self._A__y) #Accessing inside the class
a1=A()
print(a1._A__x) #accessing outside the class
a1.printresult()
Output:
10
The value of y is 20
Be First to Post Comment !
Post a Comment