У меня есть дочерний класс, который расширяет Parent, последний реализует дескриптор свойства и переписывает атрибут (self.child_prop), который есть у дочернего класса.
Я не могу изменить реализацию родительского класса, и мне нужно использовать атрибут с тем же именем.Текущая структура предполагает, что свойство 'setter' получает 'self' класса Child, следовательно, получает доступ к атрибутам верхнего уровня.Есть ли способ запретить родительскому доступу к атрибутам ребенка?
class Parent(object):
def __init__(self):
self.parent_prop = 'parent prop initialized'
def _setter(self,val):
self._local_parent_prop = val
self.child_prop = 'child value changed from parent'
print 'mro of Parent class: {0}'.format(Parent.__mro__)
print 'self in Parent belongs to {0}'.format(self.__class__)
def _getter(self):
print 'getting prop from parent'
return self._local_parent_prop
parent_body = property(_getter,_setter)
class Child(Parent):
def __init__(self):
self.child_prop = 'child prop initialized'
super(Child,self).__init__()
child = Child()
print child.child_prop
child.parent_body = 'setting parent property'
print child.child_prop # here attribute was changed from Parent
Вывод:
child prop initialized
mro of Parent class: (<class '__main__.Parent'>, <type 'object'>)
self in Parent belongs to <class '__main__.Child'>
child value changed from parent