#50 Python Tutorial for Beginners | __init__ method

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back aliens my name is David Vadim and let's continue the series on Python now the last video we have talked about oops concept right we have seen what is object and a class let's explore it more like remember when we talked about objects object will be having two stuff right one is it will be having some attributes and it will also have a behavior now when you say attributes those are variables right we can create variables and the behavior is your methods and then methods are almost same as functions but in OOP we call the math methods right now here we have to work with methods like they are not worked with variables yet so what I will do is just demonstrate that let me just remove this thing here because if we know that we can call conflict using the object itself or we can call it with your class by passing the object now syntactically we normally go for the second one which is calling the method as from the object it makes much more sense right so we remove that part and so once we got the object let's remove this as well just to clear it up Sophia now what I will do is I want to have something variables nowadays say variables what I want to is I want to have two variables one which defines the type of CPU I am working with and the amount of lamb I have example if you talk about any computer this two are very important why the CPU which you are using and RAM may be 8gb RAM or 16gb ram and in terms of CPU we have let's say I three i5 i7 or maybe some AMD CDs okay we have different parameters as well but just to keep it simple let's only focus on two things CPU and grant capacity now when you say we have two variables very define that so we can use a special method which is available in a class now when I say special you know right so the moment you say special we have talked about special variable which is understand us code name in the same way we have special method which is in it and as I especially it will be having underscores as well so let's define that so we say def underscore underscore in it you can see that we are getting suggestions as well and the moment I say enter you can see we have the function name or the method name as in it and we are passing self now C itself is not something which I'm typing there is coming automatically so it's compulsory to have those argument okay with itself I mean in here we can do something normally we use you need to ensure eyes the video but it's okay and that's what the name itself indicates right now if you are coming from C C++ or Java background we use constructor right so you can imagine this is our constructor but then it is init method but the advantage here is normally see if you want to work with conflict you are supposed to Paul country okay so it's composite' for you to call conflict but there was it to not get executed the idea behind in it is it will be getting called automatically example if I go back here and let's say I want to print something I will try it in in it so you can see I have defined a function press in it and not calling it okay so anyway the code I'm not calling in it I'm calling config because I want to execute it the moving the eyes on this code you can see it prints in in it and that too it prints two times now why two times because for every object it will get called once so you can see in total I'm getting two objects one is com1 one second is com2 so we've got two objects and it will call any two times so this is the object creation so when you say computer bracket it will instantiate we do a Paul in it for you now I want to pass some arguments okay I want to pass CPU and RAM so in the constructor itself you can pass that so in the computer bracket itself you can pass it so I can say I have a CPU here which is that say I 5 and the amount of RAM we have is 16gb the second computer we have let's say reason 3 and the amount of land we have let's say a GB so we have two computers with two different CPUs right and now how to accept this values so of course you have to pass this value see all right so I have to say self Palmer you have to pass two values you have to accept these two values so you have to say CPU comma Ram okay that's how you accept it now I know your question your question is we are passing two parameters right now we are passing two arguments one is CPU and second is RAM and we are accepting the arguments here so the thing is you are actually passing three arguments here remember we are also passing comp one the moment you say computer in the packet you are passing that comp 1 by default the same thing happens with config right when we call conflict we are not passing self or the object itself but it gets passed automatically the same way and when you say computer by passing two parameters you actually passing three the first one is the object itself the second one the value the first you hear and then ran okay that makes sense but will it work the thing is every object need to have a value because this is just an argument now so CPU and RAM is just an argument if you wanted to be a part of your object you just say because see we have to assign this value to an object right and the object here is self right so we have to say self dot that's why it right so this pump one this is our object right so we have to say self dot CPU is equal to CP you again there is no compulsion that you should be having same name you can have a different name that's fine so you say self dot Ram is equal to Ram so whatever you are passing here which is I five goes to the CPU as an argument it will be assigned to the object with itself so this self you can imagine now so in the pattern you can see we had two objects here so in total we called two objects each object will have its own variables okay it will have its own CPU it will have its own ramp and then whatever idea you pass in the arguments it will be assigned to those objects okay so we have two objects two different values and now in the conflict instead of painting this stuff I will say CPU comma ran that's what we do oh we caught an L what's wrong here the thing is this CPU is not a local variable right the CPU belongs to an object and how do we refer to object it's very simple you have to use self right that's why you are passing it so I'll just say self dot CPU and here as well you have to say self dot Ram and that's the idea behind passing this self right there passing self so that you can use it to fetch the values so you can imagine your methods and your data works together and we have a name for this concept again we'll talk about that name later to surprise but we are binding our data with every method so one object will have its own methods and its own variables right so they are working together and now let's run this code and let's see what happened so the moment you learn this code you can see it says conflict is I five sixteen and config is raising three eight so that's the idea that's how you create a variable so every object will have its own values that's amazing so that's it from this video I hope you enjoyed this video let me know the comments section because we had worked with data and we have also seen this init method they stopped more about it in fact they will be a long city's only on loops so let me do the conversation if you have any questions and those has got the channel if you have not done yet so that's everyone bye bye
Info
Channel: Telusko
Views: 275,059
Rating: 4.8853798 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python
Id: WIP3-woodlU
Channel Id: undefined
Length: 6min 45sec (405 seconds)
Published: Fri Aug 17 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.