I created a Vehicle
class and want to also have a Car
class derived from it that calls the parent constructor to set name
and color
. However I'm getting this error:
super() takes at least 1 argument (0 given)
This is my code:
class Vehicle:
def __init__(self, name, color):
self.__name = name # __name is private to Vehicle class
self.__color = color
def getColor(self): # getColor() function is accessible to class Car
return self.__color
def setColor(self, color): # setColor is accessible outside the class
self.__color = color
def getName(self): # getName() is accessible outside the class
return self.__name
self.__model = model
def getDescription(self):
return self.getName() + self.__model + " in " + self.getColor() + " color"
class Car(Vehicle):
def __init__(self, name, color, model):
# call parent constructor to set name and color
super().__init__(name, color)
self.__model = model
def getDescription(self):
return self.getName() + self.__model + " in " + self.getColor() + " color"
# in method getDescrition we are able to call getName(), getColor() because they are
# accessible to child class through inheritance
c = Car("Ford Mustang", "red", "GT350")
print(c.getDescription())
The super() method does not accept any arguments. You specify the method you want to inherit after the super() method. The above Cheese Python class inherits the values from the Food class __init__() method. We call the super() class method to inherit values from the Food class.
The super() function is used to give access to methods and properties of a parent or sibling class. The super() function returns an object that represents the parent class.
The super() function in Python makes class inheritance more manageable and extensible. The function returns a temporary object that allows reference to a parent class by the keyword super. The super() function has two major use cases: To avoid the usage of the super (parent) class explicitly.
“[Super is used to] return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class.
In Python 3 this works:
class Vehicle:
def __init__(self):
print('Vehicle __init__() called')
class Car(Vehicle):
def __init__(self):
super().__init__()
car = Car()
prints:
Vehicle __init__() called
Trying the same thing in Python 2 causes problems:
class Vehicle:
def __init__(self):
print('Vehicle __init__() called')
class Car(Vehicle):
def __init__(self):
super().__init__()
car = Car()
Throws this exception:
Traceback (most recent call last):
...
TypeError: super() takes at least 1 argument (0 given)
We need to supply the own class as first and and self
as second argument to super()
:
class Vehicle:
def __init__(self):
print('Vehicle __init__() called')
class Car(Vehicle):
def __init__(self):
super(Car, self).__init__()
car = Car()
But this is not enough:
Traceback (most recent call last):
...
TypeError: must be type, not classobj
class Vehicle:
creates an old-style class. Vehicle
has to inherit from object
to get a new-style class that works with super()
:
class Vehicle(object):
def __init__(self):
print('Vehicle __init__() called')
class Car(Vehicle):
def __init__(self):
super(Car, self).__init__()
car = Car()
prints:
Vehicle __init__() called
Having to remember these two arguments all the time is kind of annoying. Fortunately, there is a solution. The highly recommended library Python-Future allows you to use super()
without arguments in Python 2:
from builtins import object, super # from Python-Future
class Vehicle(object):
def __init__(self):
print('Vehicle __init__() called')
class Car(Vehicle):
def __init__(self):
super().__init__()
car = Car()
prints:
Vehicle __init__() called
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With