Python Tutorial for Beginners 29 - Python super()



 hey guys welcome to the next blog in Python tutorial for beginners in this blog I'm going to show you how you can use a built-in function called super in your Python classes so let's get started so to start with I have two classes here one is the parent class and other is the child class and here parent class acts like a superclass and the child class acts like a subclass which is inheriting from the parent class now both these classes have this init function and you already know what does this init function do it is the first function which is called when an instance of a class is created and here I'm just creating the object from the child class now what do you think will happen well that this constructor will be called or this constructor will be called because we are inheriting from the parent class into the child class so let's find out so I'm going to run this code and it prints child in it that means this statement under the child class init method is executed and this method is not executed now in some cases you might want to pass some parameters also into your superclass for example I'm just going to give this example let's say I want to pass a argument called name here and then I will just print the value of name how can I call this init function using this child class instance so it turns out that there is a function called super in Python that allows us to refer to the superclass implicitly now this super function is a built-in function which returns a proxy object that allows you to refer to your superclass so let's try to use this super function so you just use the keyword super and then give these parentheses because this is a function and then this per function will return the proxy object of the superclass that means I can use this dot operator to call the init method from the superclass so this notation means super dot in it means we are calling the init method from the parent class and that means we can pass the name from here so let's just pass this now I am going to just write max here and now I'm going to run this code and now you will see that child in it is printed using this statement first of all and then using the super function the init method of the parent class is called and then this name is passed here and it prints parent in it and the name whatever name we have provided here so this super function allows us to refer to the superclass now let me show you one more thing which is called the method resolution order and you can find this method resolution order by using your class names or child last name dot underscore underscore mro so just write underscore underscore Amaro underscore underscore and this statement I'm going to use inside the print function so you can see the result what it returns so I'm going to run the code now and you can see it returns this kind of order so as the name suggests mr o stands for method resolution order and this is the order in which the methods are called inside your child class or the parent class and this order means that all the method in the child class will be executed first and then all the method in the parent class will be executed now there are some rules based upon which this order is calculated and these rules are the first rule is the method inside your subclass are always called first and then whatever the method inside the base class they will be called and the second rule is related to multiple inheritance so let me just create the second parent class and I'm going to name it as parent two and here also I'm going to just write parent two here and let me inherit from the parent two also in the child class right and let's run the code once again and now you will see this order so once again child is printed and then the parent and then prayer in two and then the object so the second thing on which the amaro depends upon is the order in which you inherit from the parent class or the superclass so in our example we were inheriting from the parent class first of all and then parent too and in the same order this amaro also will be decided so all the method in the child class will be executed first and then all the methods inside the parent class whatever you use first here will be executed and then whatever superclass you right after that those method inside the class or superclass will be executed next and then at last this is the base object now let's try to just reverse this order and once again let's run the code and you will see that this order will be reversed so the method resolution order will tell you which class method will be executed first and then the order of all the methods from the super classes now one more interesting thing which you will observe here is this output so I said that this super function is used to refer to the superclass and now we have two super classes that means multiple inheritance and you will see here that only one output is printed which is from the init method inside the parent two and this statement is not printed which is inside the init method inside the parent class so how to solve this problem so if you have multiple inheritance then you need to manually call these init function using the class name so let's say I want to call the init function first fall from the parent to class I will use parent to dot in it and then you need to give also the self as the first parameter and the second which is the name in the same way you use the second class which is parent dot init function and then first parameter is the self and then you can pass any other name for example Tom here and let me run the code and you will see it calls first of all parent to init function and we have passed max here as the name and then it calls the parent one init function with the name Tom which is printed here also so this is how you can use super function in Python I hope you have enjoyed this blog and I will see you in the next blog 

Post a Comment

Previous Post Next Post

Recent in Technology News