0

I have a abstract base class that contains two attributes:

class Vehicle(metaclass=ABCMeta):
    def __init__(self):
        self._motor = self._build_motor()
        self._wheels = self._build_wheels()

    @property
    def motor(self):
        return self._motor

    @abstractmethod
    def _build_motor(self):
        pass

    @property
    def wheels(self):
        return self._wheels

    @abstractmethod
    def _build_wheels(self):
        pass

Child class example :

 class Moto(Vehicle):
    def __init__(self, prop1, ...):
        self.prop1 = prop1
        # ...
        
    def _build_motor(self):
        # concrete implementation
        
    def _build_wheels(self):
        # concrete implementation

I would like the child methods _build_motor and _build_wheels to be called at init when I instanciate a child object. Is is possible?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Gribouille
  • 102
  • 6
  • 1
    Yes, you've already done it. But you need to make sure `Vehicle.__init__` is called from `Moto.__init__`. – mkrieger1 Oct 21 '22 at 15:13

0 Answers0