Method super()
To call the parent's class method instead of the current one, we use the_super()_method in Python.
Code
We create the Cat object. The super() method inside the Cat object refers to the parent's
class Animal. So Animal constructor is called first.
Afterward, go back to the Cat constructor, initialize the age attribute and print the message.
123456789101112class Animal: def __init__(self, name): print('Calling Animal constructor') self.name = name class Cat(Animal): def __init__(self, name, age): super().__init__(name) self.age = age print('Calling Cat constructor') cat = Cat('Archie', 12)
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Still meg spørsmål om dette emnet
Oppsummer dette kapittelet
Vis eksempler fra virkeligheten
Awesome!
Completion rate improved to 7.69
Method super()
Sveip for å vise menyen
To call the parent's class method instead of the current one, we use the_super()_method in Python.
Code
We create the Cat object. The super() method inside the Cat object refers to the parent's
class Animal. So Animal constructor is called first.
Afterward, go back to the Cat constructor, initialize the age attribute and print the message.
123456789101112class Animal: def __init__(self, name): print('Calling Animal constructor') self.name = name class Cat(Animal): def __init__(self, name, age): super().__init__(name) self.age = age print('Calling Cat constructor') cat = Cat('Archie', 12)
Takk for tilbakemeldingene dine!