I have an abstract base class Bicycle
:
from abc import ABC, abstractmethodclass Bicycle(ABC):def __init__(self, cadence = 10, gear = 10, speed = 10):self._cadence = cadenceself._gear = gear self._speed = speed@abstractmethoddef ride(self):passdef __str__(self):return "Cadence: {0} Gear: {1} Speed: {2}".format(self._cadence, self._gear, self._speed)
and a subclass MountainBike
:
from Bicycle import Bicycleclass MountainBike(Bicycle):def __init__(self):super().__init__(self)def ride(self):return "Riding my Bike"
The following code will cause a recursion error, but if I remove self
from the super().__init__(self)
, the call to __str__(self):
works.
Question:
I only discovered this error when I implemented the
__str__(self):
In Python 3.x when calling the parent constructor from the child with no arguments, is passing
self
, necessary?Suppose
MountainBike
now sets thecadence
,gear
,speed
this means in my subclass the constructor will look like this:class MountainBike(Bicycle):def __init__(self, cadence, gear, speed):super().__init__(cadence,gear,speed)
notice, self
isn't being passed in the super
because to my knowledge, it can throw the variable assignments off. Is this assumption correct?