Why my_car can't be a instance of ElectricCar class | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why my_car can't be a instance of ElectricCar class

class Car(object): condition = "new" def __init__(self, model, color, mpg): self.model = model self.color = color self.mpg = mpg def display_car(self): return "This is a %s %s with %s MPG." %(self.color, self.model, str(self.mpg)) def drive_car(self): self.condition = "used" my_car = Car("DeLorean", "silver", 88) print my_car.condition my_car.drive_car() print my_car.condition class ElectricCar(Car): def __init__(self, battery_type): self.battery_type = battery_type my_car = ElectricCar( "jaguar", "red", 52 ,"molten salt")

26th Jan 2017, 8:03 PM
vivek pratap singh
vivek pratap singh - avatar
2 Answers
+ 2
in your ElectricCar class you have to specify that you're inheriting and what you're inheriting from the car class. here's a simple explanation: http://www.python-course.eu/python3_inheritance.php
26th Jan 2017, 8:15 PM
Cristi Vlad
Cristi Vlad - avatar
+ 1
when you redefine the __init__ method in the child class, you are modifying property of parent class method. my_car is a instance of ElecrticCar subclass but as you have modified the parent class method , you can't access those properties.
27th Jan 2017, 8:24 PM
vivek pratap singh
vivek pratap singh - avatar