hey guys welcome to the next blog on Python tutorial for beginners in the last blog we have seen how to use init method and self keyword in Python in this blog also we will discuss more about init method and the self keyword so let's get started so here I have a very simple class called hello and in this class I have a method called in it and I'm just instantiating this class and creating an object out of this hello class now the first question which may arise is what if I don't want to use this self keyword you can see directly that there is a red squiggly line appears here which means an error and this we can also see when we run the program so when we run the program it's going to give us this error it says in it takes zero arguments but given one now you may ask why it's saying that we have given one argument to the initialization of this hello class so as I said in the last blog that self is automatically passed when you initialize a class and that's why it says that one argument is given but in it takes zero argument because here we haven't given any argument inside these parentheses so self is absolutely important so you need to give the self now the second question you may ask is can I create multiple init method inside a class so let's see if it's possible or not so here I'm going to give this pass keyword which means that this init method is an empty method and let me create a second init method and this time I want to add one more parameter here let's say name ok so we have provided two init methods here and let's see what happens so this is the instantiation of class where I haven't given any argument to this instantiation so I'm going to run the program and what it says it says in it missing one required positional argument which is named so it turns out that it's not possible to provide multiple init methods in your Python class if you provide multiple init method in your Python class the init method which you define at last will be considered as the main init method and other will be overwritten okay so whatever init method you define at last that signature of init method will be valid and all the other init method which you will create will be overwritten by the last init method okay so let's provide the argument name here so I'm going to just provide one argument here and then run this code and it works perfectly fine okay let me just reverse this order so instead of using this init at the top I will use the init which doesn't take any parameter at the bottom now okay and we provided this argument and let's run the code and now once again we get the error which says in it takes one a positional argument but two were provided so one positional argument means that this in it only takes the self argument and nothing else but here self is provided automatically but we are providing an extra parameter here which is not required and that means this init which we have defined later is valid and this previous init is not valid so always remember it's not allowed to use multiple init method in the Python class but if you do this the last init method will be a valid init method and all the other init method will be the invalid init method now one more question you may ask here what if I want to create both kind of instances one which takes no argument here and other which takes one argument or multiple arguments here so the answer is in my previous blog in which I have shown you how to provide default value to your arguments so let's say I provide a default value to my argument here now I will create two instances of the same class one takes no argument and other takes one argument and when I run this code no error appears here okay so if you want to create an init method with multiple parameters you can either use this default value for your init method parameters or what you can do here is you can use this parameter which takes a couple so in the previous blog we have seen that we can provide an argument with Asterix in front of it and this means that we can provide multiple parameters to our method here and when we run the code it's also valid we can also provide multiple parameters here and it will also be totally valid so I'm going to run the program once again and you can see it's totally valid so if you want to provide multiple parameters to your init method you can use this kind of notation or you can provide the default value to your parameter or the last thing you can use is you can use for example this type of argument which takes the keywords so kW args here okay and this also we have seen whenever you use this kind of notation that means you want to provide the dictionary right key value pair so let me provide some kind of key value pair let's say name is equal to some kind of name here and then let me run the code and it's totally valid it doesn't give us any error so even though multiple init methods are not allowed Python you can use these type of notations in order to give variable length argument in your init method now let me just remove all the parameters from here and this instantiation from here also and let's take the next question so let's say I want to remove this pass keyword from here and here I want to initialize some values so self dot name is equal to let's say we will provide some name so I'm going to provide argument called name and self dot name is equal to name here and then let's say self dot age here and we provide some value which is not taken from the argument but we provide some static value here is it allowed yes it's totally allowed right so if you want to provide some default value for your attribute you can absolutely provide that without even passing it as an argument so it's not necessary that all the attribute values you need to provide from this argument list you can provide any default or static value here which is not coming as an argument so I hope that clarifies some more details about this init method thanks for reading I will see you in the next blog
Post a Comment