pythonclassinheritancesuper

Useless parent or super() delegation in method '__init__'


I'm working through the book Python Crash Course 2nd Edition, and I did what they outlined, but they did something that runs a warning in VS code (Useless parent or super() delegation in method '__init__'). They don't go over how to fix it, and I don't think it does anything (please tell me whether it does or not), but I'd like to not have the message, and I don't want to have the same thing happen in the future.

Here is the code for the child class:

class ElectricCar(Car):
    """A child class of the parent Car, specific to electric cars."""

    def __init__(self, make, model, year):
        """Initialize attributes of the parent class."""
        super().__init__(make, model, year)

Here is the code for the parent class, if it's needed:

class Car:
    """A class to represent a car."""

    def __init__(self, make, model, year):
        """Initialize attributes to describe a car."""
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0

    def get_descriptive_name(self):
        """Return a neatly formatted descriptive name."""
        long_name = f"{self.year} {self.make} {self.model}"
        return long_name.title()

    def read_odometer(self):
        """Print a statement showing the car's mileage."""
        print(f"This car has {self.odometer_reading} miles on it.")

Thanks for any and all help.

P.S. I apologise if the code blocks don't look like code. I clicked on the 'Code block' button when the code was selected, but while viewing, it didn't have syntax highlighting (please tell me whether I did it correctly or not, and if not, how to do it next time).


Solution

  • Let's say that you didn't add __init__ to your subclass at all. The parent __init__ has not been overridden and will be called when ElectricCar(...) is instantiated. Your ElectricCar.__init__ doesn't do anything that python wouldn't do anyway. You only need your own __init__ if you plan to do something different that the parent class.

    Just delete your __init__ method to make the warning go away.