10.Overriding Methods


#1

it shows this error=It looks like you didn't update the condition of my_car to 'like new'.

this code doesn't update condition through drive_car method of ElectricCar class instead it updates condition through drive_car method from Car class

what should i do to fix??

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, self.mpg)
    def drive_car(self):
        self.condition = "used"
        

class ElectricCar(Car):
    def __init__(self, model, color, mpg, battery_type):
        super(ElectricCar,self).__init__(model,color,mpg)
        self.battery_type=battery_type
        def drive_car(self):
            self.condition="like new"
            


my_car = ElectricCar("BMW", "silver", 150, "molten salt")
print my_car.condition
my_car.drive_car()
print my_car.condition

#2

Check the indentation of your drive_car() method in your child class - that's where your problem is :slight_smile:


#3

thanx....can u explain the code below why it works..used parent class Car instead of self in the child class version of drive_car

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, self.mpg)
    def drive_car(self):
        self.condition = "used"
        

class ElectricCar(Car):
    def __init__(self, model, color, mpg, battery_type):
        super(ElectricCar,self).__init__(model,color,mpg)
        self.battery_type=battery_type
    def drive_car(Car):
            Car.condition="like new"
            


my_car = ElectricCar("BMW", "silver", 150, "molten salt")
print my_car.condition
my_car.drive_car()
print my_car.condition

#4

Car.condition = "like new"
WWhy does it work?


#5

used parent class Car instead of self in the child class version of drive_car

The use of 'self' as the first argument in class methods is actually convention rather than requirement, but it's a convention that's almost entirely universal to Python programmers (you may notice in other languages that 'this' is used instead, and may not even appear in the list of method arguments).

If you look around on the internet you will find a lot of information on the use of self, but one article in particular I've seen cropping up a few times is a blog post by Guido van Rossum:

Another argument for keeping explicit 'self' in the parameter list [of a method] is the ability to dynamically modify a class by poking a function into it, which creates a corresponding method [...] I renamed the 'self' parameter to 'myself' to emphasize that (syntactically) we're not defining a method here.

I should also point out that when you modify Car.condition as you've done in your ElectricCar class, you are changing the class property of the parent class Car, which is where the child class is still getting its condition property from.

Your ElectricCar class needs to override the condition by modifying its own inherited condition property, which is why we use self.Condition instead.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.