Python has a special rule in it does not have access modifier, but rather it has some rules on the any method instance variable whose name begins - but doesn't end -with a double underscore (__) is private; anything else isn't private.
let's see the code.
class Mine(object):
'''
classdocs
'''
def __init__(self):
'''
Constructor
'''
self.x = 2
self.__y = 3
def print_y(self):
print(self.__y)
and let's see how we can access the private instance and the non-private instance.
import unittest
from Classes.Inheritances.PrivateVariables import Mine
class Test(unittest.TestCase):
def testPrivateVariable(self):
m = Mine()
x = m.x
with self.assertRaises(AttributeError):
y = m.__y
print("you shouldn't be able to see it")
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()