39 - Classes ( intro; self; __init__ ) | Python Tutorials

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
on two classes so up to now we've been using functions a lot anytime we've had some repetition in our code and we wanted to be able to mass reproduce specific lines of code and such or do a specific task but what if you wanted to represent something larger in your program than just one action say I'm creating a new school well I'm going to have teachers and students in that school so I need a way to represent these guys in my program and just one action or one function won't be enough to represent an entire person people are more than just one action we can eat and sleep and walk and move we also though have things that describe us brown hair or she's very tall what race they are what color eyes what gender they are etc so we need a way to represent something bigger than just a function and we do that with objects so objects have two main things that make them up attributes which are those characteristics we described like their color of the hair and stuff and methods which are those actions that we describe like that they can walk and sleep and stuff and just like functions the point of these is to be able to reuse them so I want to be able to create a bunch of different students and a bunch of different teachers or something and so we do that by creating a blueprint or a little template of our object and that template is going to be a class so you're going to create a class by typing out class and then giving it a name so for us we would want one for our students and then let's just let's just pass on this for now and we'd also want one for all teachers and again let's just pass on this for now what you're going to do is inside of these classes you're going to put all the things that every student has and that every student can do same for your teacher so let's focus on just a student for now you can actually create a new student right now by just creating a variable let's call it stud one for student one and so you create a new student from this little blueprint here by just putting the name of the blueprint and then opening and close parentheses like so and what this is going to do is create a new student objects or it'll create a new instance is what it's called and put it into this student one variable and what's cool is I can create now a bunch of different students and each of these students are going to be unique and I can show you that if I just print them all out so let's print these guys out look at the memory addresses here at the very end you can see that they're different places so I'm creating objects that I can now put attributes and methods into them that won't exist anywhere else in my code for example let's create a name variable and let's store it into this student one object right here so normally you would create a variable by just saying name and then we could put a name here let's say Jake Miller but if you want to create this name variable inside this student one object you can do so by just putting student 1 dot name equals and then whatever you want it to equal so we're creating an attribute of this object we're just creating a variable that's going to be stored within it and now I can't access this name variable anywhere else it only belongs to my student 1 so if I want to print that out and then I can run this and there you go but if I tried this on any of my other guys we would just get an error so let's create some basic things that all my students are going to have like this now you can see though that if I continue on this route could have hundreds of students in my school this is going to take up a lot of room in my code also it's very prone to errors I could accidentally do here like this and not even notice it and also if I gave this class to someone else because I haven't specified anything in my class and I'm just letting people create whatever they want into them then people could do stuff like this and so now again down in my code if I'm getting student threes name I might be expecting to just put name but it was instead input it and as this so we need a way to make all these variables more consistent so one way we could do that is just create a function so we'll just say set info and input in our students and then input in their stuff and now I can just use student and set up the name to equal the full name that they input it in to set up the age to equal the age that they input it into my function and to create a variable called year and assign that to whatever year they input it into my function so now instead of these six lines of code we can just use our method so let's use set info and input in our student so student 1 and then put the name age and classification and then I can do this for my second person so this not only saves me space but it also keeps my code more consistent because now when I'm inputting into this method it's always going to be saved as name age and year the person can't manually change it or accidentally misspelled it the only problem with this though is that I had to create this method in order to get this consistency our class should have been the one to have this implemented into it right because this is our blueprint so it should have all the core things already in it so that we don't have to do all this mess and create all these variables and that also I should have some standards that they should have to follow like that all the students names should be stored in a variable called name and their age and age etc that way that if anyone uses my student class or any other school does they know that they can always access a student's name by typing out name and that that's never going to change so this method instead of being here should have been in my class like so that way if I give my class to someone else they're going to already have this nice method to set everything up with now that this method is in my class though it's no longer accessible to me on the outside as I'm doing down here and calling these guys so instead of calling them like this we're going to have to first go through the student class here so if we just put student dot and then now we can get set info and then fill out everything else so student dot set info and now this should work just fine and when we point down one of my students let's print out student two's name we can run this and notice Alice carry prints out just fine now you can also access this method though with the instance variables this student want in student two but if you do that so if I put student 1 here and also I put student 2 here when you call a method in a class with an instance it's automatically going to pass itself as the first argument in that methods parameter list so what I mean by that is I no longer have to put student 1 here I can get rid of it in fact I have to get rid of it otherwise I'd get an error as you can see if I let's leave it on and let's run this notice my method says that it takes four positional arguments but five were given so if we check we can see that indeed it accepts in for positional arguments but when we look down at the call it says five were given so we count one two three four well where's the missing fifth one again it's because these instances when they call methods in a class automatically pass themselves in so by doing it again you're actually passing in yet another variable so we're going to get rid of that there and now I can run this and look it works perfectly fine so if you got rid of this guy this would give you an error because now student 1 when you call this set info is going to be placed into F name and then Jake would be placed into age 17 into you and now we have an extra argument here so this would give you an error always make sure to have a variable to accept in that instance right here now traditionally and by convention this is always called self so that's all that that means if you see this in Python self is just a variable that's going to accept in the instance and it's just so that you can edit its information because imagine if you had two input in the instance doesn't this seem kind of redundant we're already calling it with this student so shouldn't it already know that we're going to be editing or accessing this students information it should so again to make this really cement let's do a simple method here that would get the last name of a person so let's call last name and again we want to accept in an argument here to accept in our student and then we'd want to get that students last name so we'd get the student's name and then we would just split it up on a space so that we would get the last name in the first name because it's going to split where this space is and the last name would be index one because index zero would be the first name so let's put index one there and we'll just return this information and now again when you call this guy so down here if I do student 1 dot last name I don't have to put anything in here because student 1 is automatically going to be put in there and set to this student variable right here so we can point out this guy and we should get Miller as you can see right there so now it probably makes sense why earlier when I was calling this set info method and I was doing it like this where I said student dot set info and then input it in the student 1 and then all the other arguments same thing can be done with our last name student dot last name and then input in the student we want to get the last name of so student 1 here so actually these two lines of code do the exact same thing and actually when you call a method with an instance variable Python is going to convert this into this and that is why we have to always provide a first argument usually again traditionally called self right here otherwise we'll get an error because we're trying to pass in an argument and we want accepting it so always make sure you have at least one argument to accept in your instance variable there's ways to create functions that don't do this and we'll talk about that in the future there's one more thing I want to do before I leave though notice how we're able to create two students without setting up so what if people used my class right here and they created two new students but they forgot to or didn't know that they had to use set info and then input in their information to students now exists it don't have any information on them at all so that doesn't make sense every time we create a student we want to at least set up these three basic things so this might sound familiar if you come from other languages this might sound like we need a constructor and that's exactly what we're going to do in Python we use a special method called a knit as our constructor so we create this by doing two underscores I n I T and then two more underscores so this a knit method it's what will be called when you initialize create a new instance out of this class so again make sure that you always pass in at least one argument self but you can also pass in other arguments as we're doing here and those arguments are going to be passed in into these parentheses right here so let's just go ahead and copy these and paste them right into here just like that so now I can't create a student without inputting in anything if I try to do this notice I would get an error that said hey you're a knit method is missing three required arguments full name age and the year which is exactly what I want and now it can access any of this stuff just like I was doing before so let's put that there and run this and you can see that indeed we get junior for the year of student two and for the last name we get Miller for student one so everything is working tip-top there's still a bunch to cover on classes and I'll talk about those in the next video but for now thanks for watching
Info
Channel: SimplyCoded
Views: 11,784
Rating: 4.9125681 out of 5
Keywords: program, programming, code, coding, script, scripting, learn, how-to, beginner, free, start, basic, basics, best, hd, high, quality, voice, tutorials, series, playlist, examples, snippet, gist, simple, simply, simplycoded, jeremy, england, py, python, python3, class, classes, init, dunder, special, magic, constructor, methods, variables, names, initialize, instance, object
Id: KzZhAqu3s4M
Channel Id: undefined
Length: 13min 1sec (781 seconds)
Published: Mon Jul 03 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.