Skip to content
Advertisement

How to print an instance of a parent class in base class?

I am able to print the name of Class Student when I inherit the class into Sports and define the values in the init method. but I am not able to figure out how to print the name given when Student class is created without defining the name in Sports class,

When I comment out the Student.__init__(self, 1, "name1", "addr1", 123456789, "std2@gmail.com", 1, 2021, "english") and define the values when the object is created, i want to print out these values.

JavaScript

When I comment out the Student.__init__(self, 1, "name1", "addr1", 123456789, "std2@gmail.com", 1, 2021, "english") and define the values when the object is created, I want to print out those values.

BTW, I am learning inheritance and new to python, and I could not understand why python is not able to print the instance of Student class as I have inherited it, maybe because it is not class attribute? Please let me know…

Advertisement

Answer

Normally a subclass __init__ method takes all the parameters of its superclass, plus any additional parameters the subclass needs. Then it can pass the common parameters to the superclass’s __init__ method.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement