Я хочу повторно использовать код. В моем базовом классе есть абстрактный метод и метод publi c. Для метода Publi c требуется переменная, сгенерированная в __init__
. Если я создаю абстрактные свойства, будет повторяющийся код.
from abc import ABC, abstractmethod
class Base(ABC):
@abstractmethod
def _load_cache(self):
raise NotImplementedError
def start(self):
# Other tasks
self._load_cache()
print(self._name) # I don't know how to deal with self._name. For me this is incorrect. PyCharm lint shows
# Unresolved attribute reference '_name' for class 'Base'
class A(Base):
def __init__(self, name):
self._name = name
self._cache = []
def _load_cache(self):
self._cache = [1, 2, 3] # load from DB
class B(Base):
def __init__(self, name):
self._name = name
def _load_cache(self):
self._cache = [4, 5, 6] # load from DB
B('test').start()
Спасибо