2

In the following codes, I kept on getting the same error although I rechecked it for more than 15 minutes. For your info, I ran it on sublime text and the error:

TypeError: super() takes at least 1 argument (0 given)

The code is as shown below:

class Car():
"""A simple attempt to represent a car."""

    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0

    def get_descriptive_name(self):
        long_name = str(self.year) + ' ' + self.make + ' ' + self.model
        return long_name.title()

    def read_odometer(self):
        print("This car has " + str(self.odometer_reading) + " miles on it.")

    def update_odometer(self, mileage):
        if mileage >= self.odometer_reading:
            self.odometer_reading = mileage
        else:
            print("You can't roll back an odometer!")

    def increment_odometer(self, miles):
        self.odometer_reading += miles

class ElectricCar(Car):
    """Represent aspects of a car, specific to electric vehicles."""

    def __init__(self, make, model, year):
        """Initialize attributes of the parent class."""
        super().__init__(make, model, year)

my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
Zen_Master
  • 21
  • 7
Alex Koh 1
  • 39
  • 1
  • 1
  • 3
  • 1
    please fix your indentation of the Car class – James K Aug 21 '16 at 01:43
  • 2
    Can you check that you are really using python 3 and not python 2. On many systems running "python" gets python2. `super` is different in python 2.7 and 3.x – James K Aug 21 '16 at 01:47
  • 1
    That is in fact the error message `super()` produces when you use python2 instead of python3. – Aran-Fey Aug 21 '16 at 01:56
  • After fixing indentation I ran code in Python 3.4 with no issues. – Steven Summers Aug 21 '16 at 06:51
  • @StevenSummers: I just realised that my subline ran python 2 by default without realising it in the first place. Now I am trying hard to change my subline so that it runs python3 by default~ – Alex Koh 1 Aug 21 '16 at 08:17

1 Answers1

4

The problem here is a fairly well documented one on StackOverflow. But I'll explain how you are using super() incorrectly. You're using what's called Old Style classes, while trying to use super(). New style classes inherit from object and can be used in Python 2.2 and up (Python 3 exclusively uses New style classes).

Your Car class declaration should look like this -> class Car(object): (Car inherits from the object built-in), with your super call having the class the object is in, and self passed in as arguments:

super(ElectricCar, self).__init__(make, model, year)

Now, if we print out the type of object my_tesla is:

>>> print type(my_tesla)
<class '__main__.ElectricCar'>    

We can see it's of type ElectricCar.

Now why is all this important? Well there are a few key differences between the styles. In the Old style, the class and the objects it defines for instantiating are of different types. In Old style classes, instances are always of type instance, regardless of their class. With New style classes, an instance is generally going to share the same type that it's class has. Examples:

Old Style ->

>>> class MyClass:
    pass
>>> print type(MyClass)
>>> print type(MyClass())
<type 'classobj'>
<type 'instance'>

New Style ->

>>> class MyClass(object):
    pass
>>> print type(MyClass)
>>> print type(MyClass())
<type 'type'>
<class '__main__.MyClass'>

Please refer to Python's official documentation on super().

Community
  • 1
  • 1
ospahiu
  • 3,465
  • 2
  • 13
  • 24