Reputation: 71
class car(object):
"""A simple attempt to represent a car."""
def __init__(self,make,model,year):
self.make=make
self.model=model
self.year=year
def get_descriptive_name(self):
"""getting full name of the car"""
longname=str(self.year)+" "+self.make+" "+self.model
return longname
class battery():
"""defining the new battery class"""
def __init__(self,battery_size=70):
self.battery_size=battery_size
def describe_battery(self):
"""Print a statement describing the battery size."""
print("This car has a " + str(self.battery_size) + "-kWh battery.")
def get_range(self):
if self.battery_size==70:
range=210
elif self.battery_size==90:
range=270
message = "This car can go approximately " + str(range)
message += " miles on a full charge."
print(message)
class ElectricCar(car):
def __init__(self,make,model,year):
super(object,ElectricCar).__init__(model,make,year)
self.battery=Battery
my_tesla=ElectricCar('tesla','benz','2016')
print my_tesla.get_descriptive_name
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
Above code throws
Traceback (most recent call last):
File "python", line 27, in <module>
File "python", line 25, in __init__
TypeError: super() takes at most 2 arguments (3 given)
Above code throws the error showing the above one which I have posted. As it is throwing the super()
argument error. How to solve this error. In which the 25th line is:
super(object,ElectricCar).__init__(model,make,year)
and 27th line is
my_tesla=ElectricCar('tesla','benz','2016')
Upvotes: 2
Views: 4930
Reputation: 1610
You are doing mistake at the time of calling super(). super() takes two parameter, first one should be the type and another one should be the instance .
So your code
class ElectricCar(car):
def __init__(self, make, model, year):
super(object, ElectricCar).__init__(model, make, year)
self.battery = Battery
should be
class ElectricCar(car):
def __init__(self, make, model, year):
super(ElectricCar, self).__init__(make, model, year)
self.battery = Battery
Please note that __init__() parameters order is different also.
Upvotes: 4
Reputation: 387745
You are using super()
incorrectly. The first argument should be the current type, and the second argument the current instance. So it should be like this:
super(ElectricCar, self).__init__(model, make, year)
Note that object
does not belong into any parameter here.
Once you have fixed that, you will get a new error which is that Battery
does not exist. In order to fix that you need to change it to battery
and you will also want to call it to actually create a new battery
object:
class ElectricCar(car):
def __init__(self, make, model, year):
super(ElectricCar, self).__init__(model, make, year)
self.battery = battery()
Upvotes: 0
Reputation: 1981
The problem was in the order of the arguments of the super
call. But there are others problems on your code that I have fixed below:
#code throws super argument error
class Car(object): # Class name should be upper case
"""A simple attempt to represent a car."""
def __init__(self,make,model,year):
self.make=make
self.model=model
self.year=year
def get_descriptive_name(self):
"""getting full name of the car"""
longname=str(self.year)+" "+self.make+" "+self.model
return longname
class Battery(object): # Upper case
"""defining the new battery class"""
def __init__(self, battery_size=70):
self.battery_size=battery_size
def describe_battery(self):
"""Print a statement describing the battery size."""
print("This car has a " + str(self.battery_size) + "-kWh battery.")
def get_range(self):
if self.battery_size==70:
range=210
elif self.battery_size==90:
range=270
message = "This car can go approximately " + str(range)
message += " miles on a full charge."
print(message)
class ElectricCar(Car):
def __init__(self,make,model,year):
super(ElectricCar, self).__init__(make,model,year) # Fix super call and init order of params
self.battery=Battery() # Upper case and missing ()
my_tesla=ElectricCar('tesla','benz','2016')
print my_tesla.get_descriptive_name
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
Upvotes: 2