Well, I have almost the same question, except one detail: I need to get private values of the base class. Code:
class Parent(object):
def __init__(self):
self.__field = 13
class Child(Parent):
def ChildMethodWhichUsingParentField(self):
return self.__field
c = Child()
c.ChildMethodWhichUsingParentField()
Interpreter output:
Traceback (most recent call last):
File "foo.py", line 20, in <module>
c.ChildMethodWhichUsingParentField()
File "foo.py", line 16, in ChildMethodWhichUsingParentField
return self.__field
AttributeError: 'Child' object has no attribute '_Child__field'
The problem is that interpreter tries to get _Child__field
when I need _Parent__field
. I can get this value using @property
, but it will break the encapsulation. Also I can solve this problem writing self._Parent__field
but this is ugly and obviously bad code. Is there any other ways?