I have parent & a child class and trying to instantiate the child class to inherit the properties of parent class.
This code works perfectly well but want to know if there is anyway to keep the self.list1 to be private ie; if I declare self.__list1 then its not accessible by the child class.
Can I override the childclass method to keep the self.__list1 as private?
class parent(object):
def __init__(self,x,y):
self.x = x
self.y = y
self.list1 = ['a','b','c']
print('self.x',self.x)
print('self.y',self.y)
class child(parent):
def test(self):
print('In child self.x',self.x)
print('In child self.list1',self.list1)
class test(object):
def __init__(self,x1,y1):
self.x1 = x1
self.y1 = y1
def process(self):
childobj = child(self.x1,self.y1)
childobj.test()
pass
def main():
testx = test(2,3)
testx.process()
From the docs:
prefixing with two underscores invokes name mangling, It is up to your clients to respect the “privateness” of these variables/methods