+ 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")
2 Respostas
+ 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
+ 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.