Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What argument can we pass to super()?

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())
like image 891
Hussein Ojaghi Avatar asked Dec 28 '15 15:12

Hussein Ojaghi


People also ask

What arguments does super () take?

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.

What is the use of the super () function?

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.

How does super () work in Python?

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.

What is super with arguments in Python?

“[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.


1 Answers

Python 3 - nice

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

Python 2 - more work

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

super() without arguments in Python 2

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
like image 94
Mike Müller Avatar answered Dec 02 '22 18:12

Mike Müller