Transcript for:
Inner Classes in Python

[Music] welcome back aliens my name is Ivan 20 and let's continue the series on Python not to this point we have talked about different types of variables and we have talked about different types of methods right so we can't write readable and methods inside a class right so a class will have variables and methods the question is can we have a class inside a class that sounds weird right why would you want we'll have classes in a class so let's try to understand that so let's say I have a plus year and this class name is to deny the way we have done earlier and we talk about students we have different types of variables right let me define that function first and the variables which I want here is self first I want and name of course in the last example we have taken marks but here that sticks something different I would take a name of a person and that will be coming from a user of course or from vindicate object with same name the second thing I want is roll number and I want these two details and then I will assign name here and I will say self dot roll number now we have used to it right so I would say roll number right now once you record these two variables now what I want is I want to print them the way you print this values is by defining some functions right of course you can do that directly example if I create object here so let's say the object is s1 and how it's a student and when you create this object you have to pass values as well right the first one I want have is Naveen and rule number is related to the second object we want here is s2 and we'll say student in which will be having two things of course I would say this is also Jenny and the roll number is let's say 3 so I have Naveen and we have Jenny here we call two and three okay so let's say I want to print I want to print s1 so I want to print all the details about s1 okay I want to print the name and roll number so that way you do that is by saying s1 dot name and we can say s1 dot roll number so this is how you can print the values right let me run this code and you can see it works recorded here details about s1 now what I want to do is I don't want to see this doesn't look good right what you should be saying is you should say s1 dot show so when I say this will not show if it should print all the details about s1 maybe it has so number name or different variables right so of course we need to define that function or method years I will set F so so this will print self dot name and will itself dot roll number so we are planning this to data right it should work let's run this code on you can see we got the same data now let's say student also has a laptop of course right when you say you are doing IT in fact we have done that right so you are a student of the disco you will be using a laptop to run your code or the extra doesn't matter and let's say we want laptop here so of course I want to know which laptop we are using so I want to know your conflict now in that case I will also ask a user hey tell me which laptop you're using the problem is when you talk about laptop the different things important right it's not just about brand you will not simply say here I have HP laptop I have a Lenovo laptop you will also mention the configuration may be CPU and run that's very important maybe any i5 CPU with let's say 8gb RAM is enough so I want to know your conflict so what you will do when you pass three variables like you will pass a brand HP and then you will also pass the CPU which you are using let's say I five or you will also pass let's say them of 8gb yes you can do that ok so I have to accept this variables here I have to accept that here right and we can do that the other option which we have is to be different ok so another option which we can do here is we can create a class ok we can get a software class as laptop or you should have creating a class outside this student we can also create laptop inside student so a laptop will be only used by student and in this case some surplus and there's a laptop so we are creating a laptop class inside a student so that we can get that group ok so we have to also say def here and we have to say in it right and then let's create variables so the first variable we need here is brand and let's say the brand is HP that's the first thing we need second thing we need is the CPU which you are working with I would say CPU is I five and the third one is RAM let's say GB RAM so we got these three things right now we got these three variables and where to create the object of it so we can create the object here itself so we can create the object inside the init and normally that's what we do so if you want to create a object of laptop we should be creating that object inside the outer class okay so the object of laptop will be the inside the student class the way to that is very simple here you will say self dot L ap you can create any variable you want and you will say laptop or we are getting ed also going to say self dot laptop ok this works so you can see in the constructor itself we can define our variable so in the outer class you can define a variable lab and you can the object so if anyone who want to use a laptop you have to say this student object dot L ap example outside if I want to use it I will say s1 dot L AP dot whatever brand you want to fetch so you cannot simply sell laptop time you have to say s1 dot lab dot pland because the lab ops it is inside the student class this is one way what if you want to create another object of it let's say I don't want lab I want so you can simply say lap 1 is equal to s 1 dot lab you can get another object for s 2 you can say lab 2 is equal to s 2 dot lab because for every object you will get different type of object okay trust me this is how it works in fact you can also check that we got two different laptop object here we will print lab 1 and let's spin laptop so let's bring the IDS of lap one lap two we got two different objects here so this works ok this is where you create in a + ok so we have a laptop as an inner glass of a student right you could have done the outer class as well I mean you could have to do different classes that you want that work so but sometimes when you know that this class will be used only for student noting else then you don't have to create a separate file for that you can do that in student class itself now can I click create the object outside let's say I don't want to get out of laptop inside the student glass can if we do it outside directly let's try so I will say that one is equal to I will say student dot laptop now why shouldn't here because you cannot simply access laptop directly write because laptop class belongs to a student class so we have to always say student dot laptop constructor you cannot simply create directly ok so this is one thing you have to remember you can create the object of inner class inside outer class or you can do that outside the outer class provided you are using the other class names I have to say student dot laptop ok this works in fact just to enhance the example what I will also do is we can create a show method for a laptop as well so we can say show for laptop and you can see we have to show methods one is student and what is laptop ok so in such students we have show method inside laptop also we have show but these are two differential methods the show method of student will print the data about a student and this show method of laptop will print the show method of student I know that mix attacks weird so let's do that so I will say print self dot brand self dot CPU and self dot ran because I'll be attending this data so this show is different from this show let me now print laptop here let me on this quote on you can see it prints only the name and the roll number I also want to pin the laptop oh you need the object right we have removed that statement because if you want to access laptop you have to say self dot lap is equal to laptop what you have to say self so this is important that after printing this will call lap dot so because we also want to do that so it will say self dot laptop ok so we are printing data about the student and laptop as well so this should work let's run this code and it work you can see we caught data about student and laptop as well so this is how you create the class inside a class so that's it I hope you enjoy the session where we have talked about how you can create a class inside a class which is also called as in a class right so that's going to be one I hope you enjoy let me know in the comment section and do subscribe for for the videos thank you so much bye bye