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)
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.
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 .
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.
In Python, super() has two major use cases: Allows us to avoid using the base class name explicitly. Working with Multiple Inheritance.
__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__
.
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