Overriding methods has me lost


#1

I understand the idea of overriding methods, but the syntax is bewildering to me. I've been trying to call the drive_car() method for my_car and I'm ... well ... just so lost.

My code (with distracting bits edited out) :

class Car(object):
//////////
def drive_car(self):
self.condition = "used"

class ElectricCar(Car):
//////////////
def drive_car(self):
super(self).drive_car()
condition = "like new"

Can anyone help me understand the RULE for doing this? It would be helpful to know what needs to be passed to each method as well - if anything.


#2

If it helps you think about it better. Remember that Python is primarily a OOP language. So everything is an object, and you can use it any where you see fit as long as you remember your object. Meaning if it is not in scope you don't remember it.

A super call is akin to recalling an old memory when you over ride something.

When you call a super method you have to tell it what you are trying to remember and where to place that data.

super(class_to_look_at, place_to_put_info).method_to_remember()

Does that help?

EDIT
So your super call should be something like

super(Car, self).drive_car()

#3

In the end, this was what worked for me:

class Car(object):
condition = "new"
/////
def drive_car(self):
self.condition = "used"

class ElectricCar(Car):
/////
def drive_car(self):
super(ElectricCar, self).drive_car()
self.condition = "like new"