Let's Learn Python - Basics #8 of 8 - Classes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
good morning fellow programmers thank you for joining me I'm t-pain and welcome to another let's learn feel free to ease this skip ahead feature on the right hand side to jump ahead to any specific sections or examples today we'll be using Python 2.7 point 4 you can download it from Python org slash get it today we'll be focusing on classes this will build heavily on past lessons so feel free to go back and watch them again if anything if I'm clear alright so classes what are they so classes are way of packaging variables and functions together for instance say we wanted to create a character and a video game this character would have attributes and actions that he can do for instance he'd have health he have a weapon to start out with etc and these would be his attributes or variables that we store specific stuff in and the things that he can do are specific functions like he can attack he can run he can explode who cares so how do we use them well let's go ahead and create on our desktop a class file and I'm just going to do that doing by creating a text document we're going to call this class's underscore example dot py ok it's asking me if I want to change the file extension click yes all right and then I'm going to go ahead and right click and edit with idle all right so I have idle open um I have the Python file down below and then the shell up above all right so let's go ahead and begin with typing a very short classic example the way you start out declaring a class is using keyword class it's all lowercase so CA ll I can't spell CLA SS space and then typically with classes you'll always always want to start out the first letter uppercase the rest of it lowercase unless there's multiple words then you make those uppercase start out uppercase as well so we're going to type test capital T lowercase e s T : enter and just type Pass PA SS as you may remember from before that's just placeholder' doesn't do anything just says hey just skip past this Python alright and after that we're going to type X equal space and then test open closed parentheses just like we would for calling a function and then press Enter and we're done save it and then press f5 on the keyboard or you can go up to the run on the menu bar and then run module right there alright and it worked alright so there was no errors returned and it just created that class then the program ended cool that's exactly what we wanted excellent work all right so within classes you'll notice that you'll be using this key word self and it'll be referring to anything that is its own function or its own variables so if you're ever having any issues with creating classes double check that you're using the key word self within the arguments of the functions self is a way of saying hey this function is attached to me or this is my function the same goes for variables which we're going to be creating in just a bit so for now we're going to ahead and clear out this file and we're going to start a new class this one's going to be a class uppercase P lowercase H : enter and then we're going to begin declaring our function so we start with d EF space lowercase print uppercase ham and then begin the arguments and here the very first argument we're going to put in itself we're not going to put in any other arguments but if you were to you would use a comma and then put in wherever you wanted right there okay so we're going to go ahead in the parentheses colon enter print and then in quotations ham enter and we're done creating our class so now we're going to go ahead and create an instance of the class in order going to do that with x equals uppercase P lowercase H open close parenthesis enter and then we're just going to call that function using the dot accessor so X dot print ham open close parenthesis enter save and then we're going to run it pressing f5 perfect so we didn't have any errors or turned and it actually printed out him our function worked exactly as we had so our class example worked great but what happens if we don't include self well let's go ahead and try that out I'm going to delete self save press f5 again to run and we're going to get a type error and notice what the air at says print ham takes no arguments but one was given that one that was given was actually the self that wasn't here so we can really quickly fix that by tabbing self back in so if you ever run into this error you'll know that you're missing self and they're probably alright so next we're going to declare a constructor or a initialization function for this class alright so what is a constructor a constructor is a function that is called when the class is created it is in charge of all the setup work for a class for example if we wanted our character to have some default health and weapons when he started the game this is where we would put them all right so we're going to change our current class to include a canoe constructor we're going to go up to the very top and press ENTER to add in another definition so def underscore underscore and Nitz underscore underscore open parenthesis self close parenthesis colon enter and next we're going to type in self dot y as equal to five save and then at the very end of the file we're going to go ahead and try to access that variable Y so what is this function that we just created this is the constructor double underscore in it double underscore is the name of the function that Python uses to construct stuff when it's initially created you're always going to want to include self in the arguments and then any variables or any functions that you wish to access within itself you're going to use self dot whatever the variable name or the function name is this is saying hey this is my variable if I were to create another variable like Z for example I'll show you what will happen at the very bottom of the file go ahead and type print space X dot Y and this will print out the Y parameter that we plugged in then after that type print X dot z save and go ahead and run this we should expect to find an error right let's look at what happens up above we had to hand print out because we ran that function print ham worked great then we printed five which was the variable that was attached to the class object and then and then we try to access variable Z however we did not include self dot in front of the Z so this variable was created locally within the function and the newest deleted after it executed so this variable Z was not stored within the class and so it returned this attribute error saying that there was no instance of Z within it perfect so that's exactly what we'd expect to happen all right so what if we wanted to access functions within other functions well it's very simple just the same way that we can access variables within the class we can access functions so right here I'm going to actually call this print hand function right here within it in the init so we should see it as soon as it's constructive it will call this function so I'm going to type in self dot print ham open close parenthesis we do not need to include self within this within the arguments it's only within the definition of the function itself and now I'm going to delete those final two lines so that we're only creating the class instance we're not actually calling the function outside of itself all right so go ahead and click Save and then press f5 on the keyboard to run it perfect so you saw ham printed out because this function was called within the initialization or the constructor of the pH class perfect great job all right so next we're going to actually create our own hero class and we're going to go ahead and clear out this file that we currently have all right so let's begin by typing class capital H for the hero : enter def underscore and Nitz underscore self and then we're going to plug in another argument called name then the parameters and then end it with a colon enter and now what we're doing is we're saying hey we're going to pass in a name now we need to store it within the character so we're going to do that by typing self dot name is equal to enter and so any name that we pass into the arguments will be stored right attached to the class perfect after that we're going to type in self dot health is equal to 100 enter and we're going to create another function within this class so we're going to go an unintended open parenthesis and we're going to type self because that has to be at the beginning of every function that within this class and we're going to type in food close parentheses colon enter and then we're going to include a simple if-elsif statement to check for what food is being passed in to the arguments so we're going to type in if open parentheses food is equal to and that's double equal to check for something and I'm going to check if it's equal to Apple in quotes in parentheses : enter self dot health is - equal to 100 why why are we taking my health this guy's allergic to apples unfortunately so he's going to die as soon as he eats an apple all right so we're going to an indent and then type Elif open parenthesis food is equal to ham close parenthesis : enter self dot health is plus equals 220 perfect and we're done creating this class we're going to unint all the way back down and now we're going to wrap up the file by creating this in actual class instance of this so Bob is equal to hero and then open parentheses and they're going to punch in his name which is Bob of course click in the quotations in parentheses enter so we're going to go ahead and double check everything now by typing print Bob dot name print bob dot health and then bob dot eat and then we're going to feed him an apple to see what happens and that Apple is just a string enter and then finally we're going to print Bob's health again Bob health and now we're done all right so to review we've declared a class definition we've declared an instance right here if Bob is equal to the hero we've printed the name we printed the health he eats an apple and then we print his health again alright let's go ahead save f5 to run it Oh so I ran into the error because I misspelled health up top so I'm gonna go ahead and fix that real quick health save and press f5 to run again perfect all right so what's returned to us Bob the name is returned first excellent that's what we had expect and then Bob's health is returned at a hundred he eats the Apple which he's allergic to and then he has zero health perfect let's go ahead and punch in ham in here to make sure that our ham is working ham save f5 run perfect we get 120 health excellent all right so we're going to wrap up this example by adding some documentation for the hero now in other examples we've had a documentation be added to within the function right here by using triple quotations and then ending it and we can punch in like whatever we want right here right you can also do that the same way for classes so we're going to have triple quote enter enter triple quotes and then punch in whatever we want worth in here so we're going to ahead and punch in a quick string saying a hero who is allergic to apples so this documentation is key because for any coders or anybody else creating instances of this down the road they have this clarify what this class actually does all right so this is the end of the Python basics series congratulations on making it so far excellent work you now know the basics and fundamental stuff that makes Python Python I will be starting up an intermediate series soon so keep an eye out for that please subscribe so you don't miss it when it comes out we'll be covering intermediate topics like object-oriented programming inheritance UML Python QT Python with Maya reading and writing files thank you so much for watching great job keeping up definitely take a few minutes to investigate this final example please leave a comment below if this helped you at all and please do me a huge favor and subscribe to my channel thank you for all your support and keep the dream alive
Info
Channel: Trevor Payne
Views: 207,440
Rating: 4.9143219 out of 5
Keywords: Functions, Self, Lets Learn, Python Programming Language, TPayne, Classes, Script, Experience, #8, Tutorial, Learn, Class, Programming, Scripting, Python, Basics, Lets, Intro, Introduction, Beginners
Id: trOZBgZ8F_c
Channel Id: undefined
Length: 13min 27sec (807 seconds)
Published: Sat Jul 06 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.