2
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')
poke
  • 369,085
  • 72
  • 557
  • 602
sravan kumar
  • 71
  • 1
  • 6
  • 1
    `class battery` should inherit from `object` too (and while you are at it change it to `Battery`). Also, `super`'s first argument should be the class itself (not an instance) and the second one `self`. http://stackoverflow.com/questions/5066315/why-is-super-broken-in-python-2-x – DeepSpace May 22 '17 at 06:52
  • Is this Python 3 or Python 2? – poke May 22 '17 at 06:53
  • @poke Python2 judging by `print` – DeepSpace May 22 '17 at 06:53
  • @DeepSpace Ah yeah, down there, missed that one. – poke May 22 '17 at 06:54

3 Answers3

4

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.

Deepak Dixit
  • 1,510
  • 15
  • 24
2

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()
VMRuiz
  • 1,931
  • 12
  • 20
0

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()
poke
  • 369,085
  • 72
  • 557
  • 602