'Python inheritance child class raising error
class student():
def __init__(self,fname,lname):
self.name = fname
self.lastname = lname
def printthis(self):
print(self.name,self.lastname)
class person(student):
def __init__(self,fname,lname,age):
student. __init__ (fname, lname)
self.umar = age
def wlcm(self):
print(self.name,self.lastname,self.umar)
e = student("HARJOT", "GILL", 20)
e.wlcm()
Can anyone please explain what I am doing wrong here it gives me an error (TypeError: student.init() takes 3 positional arguments but 4 were given) I am unable to figure out. the child class is raising the problem.
Solution 1:[1]
Are you looking for something like this?
When you want to initialize child class using parent's init, you should call -> super().init(params)
class student:
def __init__(self, fname, lname):
self.name = fname
self.lastname = lname
def pprint(self):
print(self.name, self.lastname)
class person(student):
def __init__(self, fname, lname, age):
super().__init__(fname, lname)
self.umar = age
def pprint(self):
print(self.name, self.lastname, self.umar)
s = person('c', 'mor', 27)
s.pprint()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Morgan |
