Python Attribute Error "Battery" object has no attribute "make" - inheritance

I'm a Python noob and am having some trouble with some inheritance learning. My code is throwing an attribute error.

class Battery():
    """A simple attempt to model a battery for an electric car."""
    def __init__(self, battery_size=70):
       """Initialize the battery's attributes."""
       self.battery_size = battery_size

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

    def get_range(self):
        """Print a statement about the range based on the battery size."""
        if self.battery_size == 70:
            range = 240
        elif self.battery_size == 85:
            range = 270

    message = self.make + " can go approximately " + str(range)
    message += " miles on a full charge."
    print(message)


class ElectricCar(Car):
    """Represents aspects of a car, specific to electric vehicles."""

    def __init__(self, make, model, year):
        """
        Initialize the attributes of the parent class.
        Then initialize attributes specific to an electric car.
        """
        super().__init__(make.title(), model, year)
        self.battery = Battery()

    def fill_gas_tank(self):
        """Electric cars don't have gas tanks."""
        print(self.make + "'s " + "don't need a gas tank.")

my_tesla = ElectricCar('tesla', 'p90d', '2016')
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()

I've played around with the coding and the attributes, but I can't seem to get it to run without an error. Any guidance is greatly appreciated. Traceback (most recent call last): File "C:\Users\n\Downloads\inheritance.py", line 184, in my_tesla.battery.get_range() File "C:\Users\n\Downloads\inheritance.py", line 158, in get_range message = self.make + " can go approximately " + str(range) AttributeError: 'Battery' object has no attribute 'make'

1 answer

  • answered 2018-03-13 20:22 Maurice Meyer

    make is not defined in battery. You need to pass make to Battery-class:

    class Battery():
        """A simple attempt to model a battery for an electric car."""
        def __init__(self, make, battery_size=70):
           """Initialize the battery's attributes."""
           self.make = make
           self.battery_size = battery_size
        ...
    
    class ElectricCar(Car):
        """Represents aspects of a car, specific to electric vehicles."""
    
        def __init__(self, make, model, year):
            """
            Initialize the attributes of the parent class.
            Then initialize attributes specific to an electric car.
            """
            super().__init__(make.title(), model, year)
            self.battery = Battery(make.title())
    
        def fill_gas_tank(self):
            """Electric cars don't have gas tanks."""
            print(self.make + "'s " + "don't need a gas tank.")