Код: Выделить всё
class grandparent(ABC):
@abstractmethod
def get_obj(self):
pass
class ParentBase(grandparent):
def __init__(self, obj1):
self.obj1 = obj1
def get_obj(self,func):
return getattr(self.obj1, func)
def get_base_obj(self):
return super().get_obj('base')
class child1(ParentBase):
def __init__(self, obj1):
super().__init__(obj1)
def get_obj(self):
return super().get_base_obj().child1()
class child2(ParentBase):
def __init__(self, obj1):
super().__init__(obj1)
def get_obj(self):
return super().get_base_obj().child2()
Код: Выделить всё
child_instance = child1(obj1)
child_instance.get_obj()
Если я изменю функцию на:< /p>
Код: Выделить всё
def get_base_obj(self, func_name):
return self.get_obj(func)
Я хочу, чтобы get_base_obj в родительском классе вызывал get_obj в родительском классе при get_obj вызывается из дочернего класса. Есть ли способ сделать это, или мне нужно сделать родительский.get_obj функцией класса? parentBase — это базовый класс, поэтому инициализация оттуда не будет
Подробнее здесь: https://stackoverflow.com/questions/791 ... d-function
Мобильная версия