There is no keyword like public, protected, private in Python. However you can make any hidden variable in Python.
Hidden variable can be static or instance.
You can't access hidden static variables outside the class in the same way as before.
Script1: Write an script which shows the demonstration of static hidden variable.
class A:
x=6 #static variable
__y=5 # static hidden variable
print(A.x) # 6 will be printed
print(A.__y) #error comes here
Script2: Write an script which describe how to access static hidden variables.
Static hidden variables can be access inside the class using static method as given below:
class A:
x=6 #static variable
__y=5 # static hidden variable
@staticmethod
def showhiddenvar():
print("The value of hidden variable y is",A.__y)
print(A.x)
A.showhiddenvar()
Output:
6
The value of hidden variable y is 5
Note* Static hidden variables can also be accessed outside the class by changing the name during accessing them. Actually what Python is doing when we are making any variable hidden is, it is just changing its name. So we can access the hidden variable if we know what is the way of changing the name of variables of Python.
Example if i write__x=3
then we can access this static hidden variable outside the class as ClassName.__ClassName__x
Be First to Post Comment !
Post a Comment