samedi 11 juin 2016

Python Crash Course - Inheritance from parent class (Instance has no attribute)


I'm at Chapter 9 of this book, Python Crash Course and am facing this problem where the error message is ElectricCar instance has no attribute. Error message as per quote below. I can't seem to find the issue when I've been trying to match it word for word as per the book for 2 days! Thank you very much in advance.

class Car():
    """A simple attempt to represent a car."""

    def __init__(self, make, model, year): #creates class paramenters
        """Initialize attributes to describe a car."""
        self.make = make #creating the make variable for the instance
        self.model = model
        self.year = year
        self.odometer_reading = 0

    def get_descriptive_name(self):
        """Return a neatly formatted descriptive name."""
        long_name = str(self.year) + ' ' + self.make + ' ' + self.model
        return long_name.title()

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

    def update_odometer(self, mileage): #remember to add new argument
        """Set the odo to the given value but not to roll back to 0"""
        if mileage >= self.odometer_reading:
            self.odometer_reading = mileage #remember to add new argument
        else:
            print("You can't roll back an odometer!")

    def increment_odometer(self, miles):
        """Add given amount to the odometer reading"""
        self.odometer_reading += miles

 class ElectricCar(Car):
     """Represent aspects of a car, specific to electric vehicles"""
     def ___init__(self, make, model, year):
         """Initialize attributes of the parent class"""
         super().__init__(make, model, year)
         self.battery_size = 70

     def describe_battery(self):
         """Print a statement describing the battery size."""
         print("This car has a " + str(self.battery_size) + " kWh battery")

 my_tesla = ElectricCar('tesla', 'model s', 2016)
 print(my_tesla.get_descriptive_name())
 my_tesla.describe_battery()

File "/home/user/python_work/car.py", line 48, in describe_battery print("This car has a " + str(self.battery_size) + " kWh battery") AttributeError: ElectricCar instance has no attribute 'battery_size' [Finished in 0.032s]


Aucun commentaire:

Enregistrer un commentaire