Thumbnail for super keyword in Python | Python Tutorial - Day #72 by CodeWithHarry

super keyword in Python | Python Tutorial - Day #72

CodeWithHarry

8m 52s1,609 words~9 min read
Auto-Generated

[0:00]Guys, today's video will tell you about the super keyword. Sometimes we need to call the methods of the parent class from the child class. And to do this, we use super. How is super used? What is the syntax? I will tell you this in today's video. Let's go to the computer screen and let's get started.

[0:25]So first of all, we will see this practically, what exactly is the super keyword? So see, super keyword in Python is used to refer to the parent class. Now, for example, I copy this code. So I created a parent class and inside it, I created a parent method, inside which I printed, "This is the parent method." Then I created a child class. Let me give it some space. Then I created a child class and inside it, I created a child method. Okay? Now suppose I want to call the parent method of the parent class. See, when we create a child class from a parent class, this is clear to everyone, right? That all the methods and attributes inside the parent class will come inside the child class. Will they come? Yes, they will. So now if I want to call a method of the parent class, how will I do that? I can do that with the help of the super keyword. So super keyword is useful when a class inherits from multiple parent classes and you want to call a method from one of the parent classes. Okay? Now suppose what happens is that this is the parent class. Suppose there is a parent method inside it. Now suppose I create another parent method inside this. And I also name it parent method, and I put self here. And I write print, and I write Harry here, just Harry. Now if I want to do something like this here, that I want to run the parent method of the old class, inside this parent method, then I can do that. I can say super.parent_method. Okay? So along with Harry, I also do super.parent_method. So if I run this, first of all, you see that we are creating an object of the child class. And then we are running the child method in it. So when I run the child method, this is the child class, this thing will run. Let me put this here. Along with that, what will happen is that by doing super.parent_method, the parent method of the parent class will be called. So because of this, we are seeing this. Now if suppose I do something like child_object. and then I do parent_method. Suppose I want to call its parent method. So you see that this method will be called. So first, Harry will be printed. Then what will happen is that this is the parent method, this method will be called. Okay? So what does super mean? It means go one step up, go to the parent class, and call its method. That's the simple thing. So when a class inherits from a parent class, it can override or extend the methods defined in the parent class, which I am going to tell you next. Okay? That is, if I have created an object named child_object. And I called its dot_parent_method, then what will happen? This parent method will run. But what if this parent method was not there at all? Would we have seen Harry? No. This would have directly called this method because it is not present here. I will make this 12. I will make this 1. I will put 22 after all the methods here. Okay? So you see here. When I run this, child_object.child_method, what will it run? child_object.child_method will run this method. And child_object.parent_method will run its parent method because the child class does not have its own parent method. Okay? The child class does not have its own parent method. That's why it will call its parent method. So you see parent method one. But if I had, say, Harry 2 here. Then what would happen? It would not call this method, but this method, because now it has its own method. But I want some code to be taken from the old method as well. So for that, I will use super. So I wrote super.parent_method. Sometimes what happens is that we also want to call the constructor of the super class. So then also we will use super. Like I show you. Suppose I wrote here def init. In fact, I'll do one thing. I'll give you a new, fresh example. I made a class Employee here. Okay? I said that this will be created by a constructor. Okay? I am writing fast so that your time is not wasted. Okay, I am writing fast so that time is not wasted. Now I wrote here that self.name = name. Okay? I am fast forwarding it for you guys, automatically the video. And here I am writing along with the name, let us say ID, employee ID. Okay? So here I will do alt shift down arrow key, I will do self.id = ID. Now suppose what I did here is that I created a class Programmer. Okay? And inside this, I will create init. And suppose I also need a programming language in it. So I have one way that I do what? I write something like this here. self.lang = lang. self.lang = lang. Okay? And here I will say that give me lang too. That is, give me the language too. So if anyone creates, say, A is an employee, or I'll do one thing, Rohan is an employee. Okay? And along with the employee, I will give his name Rohan Das. And after that, suppose I will give his employee ID 420. Okay?

[5:19]420 is his employee ID. And after that, Harry is there. His employee ID is I will do something like this here.

[5:33]And here I will do, let us say, 2345 is my employee ID, Harry's. Now Harry is a programmer. And employee Rohan is a normal one. Harry is a programmer. Okay? Now suppose I run this code here. Okay? So an error will come because it will say that, hey, look, we also have to give a language to the programmer, right? So this thing became wrong. So you will have to give a language here. Suppose I give Python. Okay? If I run it, it will run. There will be no error in this code now. Why? Because in the constructor of programmer, three arguments were being taken. In the constructor of employee, two arguments were being taken. So this was satisfied. It got what it wanted. And obviously, I haven't printed anything, so I'll print Rohan.name here just to see if my program has run or not. So Rohan Das is printed here. But now if suppose I want to make the most of code reusability here, I will say that, hey, look, right now there are only two lines. Maybe there were 50 lines. If there was some parsing of strings in these lines, then I would have had to copy all this logic here. So we always have to keep the DRY principle in mind. Do not repeat yourself. Don't repeat yourself. Okay? Why repeat? Don't repeat. Okay? So what we will do is, to avoid repeating ourselves, I will say here that, hey, look, super's init should be called. super.__init__. And call super's init, and give it name and ID, because it takes name and ID. Okay? So super.init, I did name and ID. And the language, I will set it manually. So I will write lang, lang. So if I do this, then will I be able to print Harry.name, Harry.id, Harry.lang? The answer is yes, I will be able to.

[7:27]I ran this function as it is from here. Actually, I made a mistake here that I did not inherit the programmer from the employee. So I should have inherited it.

[7:37]I should have inherited the programmer from the employee. So as soon as I wrote programmer employee here, now programmer is a subclass. This is a superclass. If I do super.init here, then what will happen is that name and ID, I can pass them and set them. So see, if I run this now, you will see Harry, 2345, and Python. So by doing super.init, I called the constructor of the parent class. So you can do this. So I hope you guys have understood this thing. And along with that, guys, if you haven't accessed this playlist, then definitely access it. You just have to do what? See, go to the first video and put your own review. Many people haven't put a review yet. Put a review, guys. It won't work like this. Okay? You have to put a review for those who are new to watch this course. I really want you guys to write your review that, hey, you accepted the 100 days of code challenge. You have now reached day 72. You have watched 72% of this course, which is a good amount of videos. So you know how this course is. So you can write a review, you can give stars out of five, how many stars you want to give. Along with that, you can write here that, hey, how was the course? You have to write it. Okay? So for now, that's all in this video, guys. Definitely access the playlist. Thank you so much, guys, for watching this video. And I will see you next time.

Need another transcript?

Paste any YouTube URL to get a clean transcript in seconds.

Get a Transcript