Why self argument is not passed , when we used super () method | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why self argument is not passed , when we used super () method

Code from another site::::::> def __init__(self, fname, lname): self.firstname = fname self.lastname = lname def printname(self): print(self.firstname, self.lastname) class Student(Person): def __init__(self, fname, lname, year): Person.__init__(self, fname, lname) # super().__init__(fname, lname) <- or use this instead of the line above self.graduationyear = year def welcome(self): print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear) x = Student("Mike", "Olsen", 2019) x.welcome() In this code I didn't understand the #comment line why it is not possible to pass 'self' argument like this super().__init__(self,fname,lname) ^ here

17th Aug 2019, 8:27 AM
Sagar Hiremath
3 Answers
+ 2
Okay, I will try to explain(if I can). Let us first look at this line: Person.__init__(self, fname, lname) Here you are calling the __init__-method of the class person. It has three parameters (self, fname, lname). So you need to call it with those three parameters where "self" refers to the instance it should be used with. Now let us look at the other option and how that one works: super().__init__(fname, lname) Here you are first calling the (built-in) super method on an object (yes, at that point it has already been created) of the class "Student". Whenever you call an object' s method it automatically passes itself s you don't have (and are not allowed to) pass it again.
17th Aug 2019, 10:36 AM
Thoq!
Thoq! - avatar
+ 1
No problem. Btw. using super() is the preferred way of writing it. Keeps your code more flexible for when you want to exchange or rename the parent class.
17th Aug 2019, 11:11 AM
Thoq!
Thoq! - avatar
0
Thoq! Tq brooo
17th Aug 2019, 10:47 AM
Sagar Hiremath