noname
noname

Reputation: 21

I use super() function to call the __init__() of parent class inside that of the subclass,but isn't this supposed to create instance of parent class?

class Computer():
    def __init__(self, computer, ram, ssd):
        self.computer = computer
        self.ram = ram
        self.ssd = ssd


class Laptop(Computer):
    def __init__(self, computer, ram, ssd, model):
        super().__init__(computer, ram, ssd)
        self.model = model

      
lenovo = Laptop('lenovo', 2, 512, 'l420')

since the super() function returns a proxy object , which is an instance of the base class Computer in this case, isn't super().__init__(computer,ram,ssd) supposed to create an instance of Computer class and not Laptop class. Is self passed as an argument to __init__ in this line super().__init__(computer,ram,ssd) ?

Upvotes: 1

Views: 47

Answers (1)

ronpi
ronpi

Reputation: 490

The __init__ method is an instance method and it is executed after the instance have already been created.

The class method __new__ is responsible to create a new instance of the specific class, and obviously called before the init method of the instance.

By this logic, calling to super().__init__() should only initialize parameters of the base class.

Upvotes: 1

Related Questions