Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python's super() , what exactly happens? [duplicate]

Tags:

python

super

I am trying to understand what objects are created when we instantiate the child class in Python, for example:

class Car():
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year

class ElectricCar(Car):
     def __init__(self, make, model, year):
          super().__init__(make, model, year)

my_tesla = ElectricCar('tesla', 'model s', 2016)

When we create the object my_tesla, we instantiate the class ElectricCar by calling the constructor of this class, which in his turn calls the constructor of the parent. How does it happen? Right now I have two guesses:

1) super() is just the reference to the parent class, so we call the constructor of the parent by "super().init(make, model, year)" instantiating our child class. As a result we have just ONE object of our class ElectricCar().

2) super(), calls the constructor of the parent, create the object of the "Car" class, then we call the constructor of this object, by "super().init(make, model, year)". As a result we have TWO objects: one object of the class Car() and one object of the class ElectiricCar, in our case they are the same though.

Which one is correct? If neither then please explain what exactly happens in the:

 super().__init__(make, model, year)
like image 335
Victor Di Avatar asked Jul 04 '16 14:07

Victor Di


People also ask

What does the super () do in Python?

Definition and Usage. 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 with multiple inheritance?

super() returns the parent, but in the case of multiple inheritance, it returns the first parent. Because Triangle was defined before Square in the list of the RightPyramid , the super() method calls the . what_am_i() of the Triangle .

What does super () __ init __ do?

When you initialize a child class in Python, you can call the super(). __init__() method. This initializes the parent class object into the child class. In addition to this, you can add child-specific information to the child object as well.

When and why do we use super () in Python?

In Python, super() has two major use cases: Allows us to avoid using the base class name explicitly. Working with Multiple Inheritance.


1 Answers

__init__ isn't a constructor, it's an initializer. By the time __init__ is called, the objects has already been constructed (via __new__). So you get only one object, but it's initialized twice - for example, ElectricCar.__init__ may decide to re-initialize self.model after Car.__init__ has been run.

When calling super(), the appropriate baseclass is looked up in the context of the current instance. Basically, super().__init__(make, model, year) could be rewritten as Car.__init__(self, make, model, year) in your example.

This is why in earlier versions of python, the call was actually super(ElectricCar, self) - it looks up the baseclass of the current class (ElectricCar) and uses the current instance (self) as if it were an instance of that class instead.

Note that initializing doesn't mean preparing the object, it means preparing the object's state. An object is fully functional even when it does not implement __init__.


To clarify: When you call ElectricCar(), what is actually executed is close to this:

that_object = ElectricCar.__new__()  # this will actually use object.__new__
if isinstance(that_object, ElectricCar):
    ElectricCar.__init__(that_object)
return that_object

That means you have one object from the call to ElectricCar.__new__. The call to ElectricCar.__init__ will only modify/initialize that object. It may do so using other functions, such as Car.__init__.

like image 87
MisterMiyagi Avatar answered Sep 22 '22 00:09

MisterMiyagi