10. overriding methods


#1

I got the same error:

class Car(object):
condition = "new"
def init(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg

def display_car(self):
    print "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, model, color, mpg,battery_type):
self.model = model
self.color = color
self.mpg = mpg
self.battery_type=battery_type
def drive_car():
self.condition="like new"

my_car=ElectricCar("DeLorean","silver",88,"molten salt")
print my_car.condition
my_car.drive_car()
print my_car.condition


10. Overriding methods
#2

Any inputs @stetim94 @appylpye @mtf


#3

Error:

new
used
new
Traceback (most recent call last):
File "python", line 34, in
TypeError: drive_car() takes no arguments (1 given)


#4

should be,

def __init__(self, model, color, mpg, battery_type): #note the __ __

Here as well,


Also, your drive car function should take one positional argument self this goes for all funtions in a class,
self must always be there in Python.

def function_name(self):


#5

Hi @codesurfer82527,

@dave.n's post explains the problems. In particular, this error message ...

TypeError: drive_car() takes no arguments (1 given)

... was due to your having omitted the self parameter from this method header ...

    def drive_car():

When you make this method call ...

my_car.drive_car()

... the Python interpreter uses my_car as an argument to the drive_car method. Without there being a parameter, such as self, in the method definition to correspond to that argument, an error is raised.


#6

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