Chidananda Nayak
Chidananda Nayak

Reputation: 255

O/P error in Python Inheritance?

I am practicing Python Inheritance and have written this code,

class College:

    def __init__(self,clgName = 'KIIT'):
        self.collegename = clgName
    def showname(self):
        return(self.collegename)

class Student(College):
    def __init__(self,studentName,studentRoll):
        super().__init__(self)
        self.studentname = studentName
        self.studentroll = studentRoll
    def show(self):
        print(self.studentname,self.studentroll,self.collegename)



p = Student('ram',22)
p.show()

i want answer to be like ram 22 KIIT but its showing ram 22 <__main__.Student object at 0x00000238972C2CC0>

so what am i doing wrong? and how can i print desired o/p ? please be guiding me, Thanks in advance.

@Daniel Roseman Thanks sir for clearing my doubt, so if i would like to get the same result by this way what i have to do, not its showing super.__init__() needs a positional argument

 class College:

    def __init__(self,clgName):
        self.collegename = clgName
    def showname(self):
        return(self.collegename)

class Student(College):
    def __init__(self,studentName,studentRoll):
        super().__init__()
        self.studentname = studentName
        self.studentroll = studentRoll
    def show(self):
        print(self.studentname,self.studentroll,self.collegename)


c=College('KIIT')
c.showname()
p = Student('ram',22)
p.show()

Upvotes: 1

Views: 35

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599778

You're explicitly passing self to the super __init__ call; that is taking the place of the clgname parameter. You don't need to pass it there, it's just like calling any other method so self is passed implicitly.

class Student(College):
    def __init__(self,studentName,studentRoll):
        super().__init__()
        ...

Upvotes: 4

Related Questions