How can I access the private variable 'number' of class B in another class A, in the below code?
class A:
def write(self):
print("hi")
'It should print the private variable number in class B'
def check(self):
print(B.get_number(self))'error occurs here'
class B:
def __init__(self,num):
self.__number = num
'accessor method'
def get_number(self):
return self.__number
#driver code
obj = B(100)
a = A()
a.write()
a.check()
The error message I get is 'A' object has no attribute '_B__number'
B, don't use name-mangling to hide the attribute. If someone else wroteB, then maybe there's a reason they are hiding access.Ato a function that expects an instance ofB.