 Hello everyone, Dr. Lehman here, back with a really important concept that's going to carry you through the rest of the semester And that is Python classes. So in this video, we're going to get started on Implementing some Python classes So last time, in the last video, we talked about data types and their relation to Python and how Python uses them in memory Well in Python, there is a mechanism, as we mentioned last time, for implementing your own data types And that mechanism is called a class. Now if you have a Java programming background You're very familiar with classes, and maybe you got exposed to classes before That's fine. We're going to go over them again And we're going to make sure you really understand them because throughout the rest of this semester, the rest of this course You will be writing classes and you will be using them to solve more complex problems And if you remember last time, we asked the question Why do you have data types in programming? And the answer was, well, to abstract details away So you can focus on solving bigger problems. That's what we will use classes for in this course Which is kind of wrap up some details of things in little packages called classes and data types called classes And then we'll use them to solve other bigger, more interesting problems Okay, so In Python, classes define a new data type Just like you have string class, into class, etc Allowing new instances of that type to be used in code Classes have instance variables for storing data, right? Remember data types have two things They have data and they have operations Well in Python classes, the data are kept in what we call instance variables We're going to talk about instance variables in the next video And then the second thing you get with a data type are the operations you can perform with that data type and Python classes, these things are called, they're not called operations and they're not called functions. They are called methods Okay, so let's get into it What I would like you to do is fire up an interpreter Preferably PyCharm because we're going to be doing a couple of lines of code at a time and you're going to want to rerun everything So get your editor open and then anytime you see code on the screen, I want you to follow along, okay? So in your code, so type this up Here we are going to define a very, very simple class And we're going to call it student and this is going to create a new data type in Python for us to use Now this new data type that we're going to create it's only going to be there like for as long as our current Python interpreter is running it goes away, right? If you reboot your computer or Python crashes forgets all about it about this class you create All right, so how to create a class in Python? Well, you need this keyword called class Should be easy to remember and then you give it a name, okay? Class names in Python are capitalized, right? They are camel case or title case of some people call it. It's capitalized and Then inside our very basic very simple Class that we're going to call students. So we got class and then the name of the class colon We come down and we we do our four spaces in or our tab character in We are going to define a method Remember methods in class terminology are the operations of the data type There's going to be a lot of vocabulary in this Lecture so you need to Really let it sink in Maybe you need to watch it a few times because there's a lot of super important terms We're going to keep introducing this lecture and just use over and over and over again throughout the semester, okay Inside our class student where you are defining death a method Okay method is the operation on our data type But methods you can just think of them as a function from Python that is declared inside a class Okay, so it's a function. You all know how to define functions, right and what functions can do Python methods are just functions that are inside a class. Okay, everything you can do with a Python function You can do with a method. It's just that the method lives inside the class So I want you to think of classes And think of instances of classes when you're using them think of them as a little box Right and things live inside that box that you need to get access to and can use to solve problems Right so to define a method It looks very similar to defining a function. You start with the death keyword And it's tabbed over here. So you know it belongs inside the student class Then you give it a name. Okay method names function names in python should be all lowercase And then there's always always one at least one parameter and that is called self Okay, just like this self is a keyword in python Uh, we're not going to go into what it means right now. We will get to that back at the end of this lecture. Okay, so But remember remember any time you will ever define a method inside a function Self must be the first parameter and it must always be there. Okay So hop over to your code make sure you've got this typed in your code should Uh, well actually i'm going to come back here Okay, this is just the class definition if you go ahead and run your code It won't do anything right it it it should run, but there won't be any output Okay, that's because what we're defining here is we're think of it as defining a blueprint This is what a student class can do But you still we have to actually Create one of these things and use it right just like python has string classes that are defined Well one string class that is defined You still have to make an instance of a string class in order to to use the string right So here's how we create an instance of our student class We call student The name of the class. It's got to be this right followed by open parent close parent So this looks a lot like a function call or a method call and in fact, that's what it is It's calling a special method called a constructor. We will get come back to constructors in a later video. Okay next video But anyway, just know hey, I need a new student. I want to play with you call it like this Okay, this call returns an instance of the student class and assigns it to the variable x This is just like you saying x gets one or x gets the string alice right x gets student gives you a new instance of this Okay, you can have multiple instances x gets student y gets student z gets student make as many as you want Just like you can with strings or ints Now I want to call this thing. How do I call this method? You use this syntax x Dot this is the dot operator say name, okay So this calls the same name method on the instance of x So go over to your code your code should look like this Zoom in a little and if I go ahead and run this It's a little gray up here because I don't have the appropriate white space. I think Oh, no, it's a different warning ignore that Here's the code we had from the slide go ahead and run it So you can see down here the output did happen So what happens here? What's going on line six? We get a new instance of the student class and think back right think back to the last lecture Classes are like The key of serento 2020 for front wheel drive l or whatever it was When you do this you're saying hey, give me one of those you go to the heap and you get one Okay, and now I'm saying x dot say name. Whoops So this says all right take my instance my particular car of student And on that instance invoke the say name method Okay, so up here say name. What does say name do? This is just now like a function call You call this and it says oh, okay The user the programmer wants me to do say name. Where is say name? Let me go up here to the definition It does any parameter argument substitution It needs to do we'll come back to that later and then it goes inside the function and runs the function body Okay, which in this case does the print statement And then it comes back out to where it was called, right? So we leave line three we come back to line seven, right? And we're done. We continue on right. We could do more here. So Let me just print something right So we're going up here invoking this method looks a lot like a function Finishing our execution we come back down to line seven then we go on to line eight All right, that's all that's going on here. Not too bad, right? It's just kind of like a funky Function but with extra steps All right, let me get out of this go back to my slides for a second Okay So You saw the dot operator that we used before well, you have seen the dot operator before in your life Programming right like if you were using a list You could append to a list right and you called x. Here's my list x dot Append something okay Or maybe you had string string dot split string dot strip this dot operator What it does what it's really doing is it is used to call a method That is defined within a class Okay, so when you call x dot append What python does is it goes to its class definition for the list Goes down finds the append method and then invokes it right and the method is just a fancy function Okay, so that's what the dot operator does it calls things calls methods and later we'll use it to access instance variables on instances Of classes okay, let that sink in Right understand what the dot operator is doing. It's going to The class instance and calling a particular method on it. Okay. Now, you know how those methods are defined They're defined this way All right, let that sink in a little bit. You can also have instance methods with uh Arguments we'll do that here in a little bit. Okay All right, but let's go back and play with Some more classes right so what I want you to do is Copy and paste that student class you just created And make a different class called another student. Okay, just change the name to another student but have their say name Print out something different Right So and then we're going to call this code x gets student x dot say name y gets another student y dot say name Okay, so let's go do it together Okay All right, so here's my code What I said to do is copy this stuff Okay, copy and paste And I said rename this to another student And change uh This to print out a different name Alice Okay, so these look very similar Right, but they are different. I am actually going to create two separate classes here And we'll prove to ourself that it is two separate classes So I'm going to create an instance. I'm going to get an instance of this other class another student And I'm going to tell it to say its name Okay, so what do you expect that the output is going to be? Well, it should print first I get x say my name is lucas Then get an value this different class and call say name and say my name is alice Right, so this is where like The methods are really important Okay So python is not going to get confused. We've got two methods here that are have the name say name But they're defined in different classes. So even though you're calling the same Looking thing down here python knows the difference It knows that when you call x dot say name It should go to student because that's what x is it's a student and call students say name y Is another student a different class. So when you call y dot say name here It's saying. Oh, wait a minute go to y Which is an instance of another student and call its Say name, okay, so let's go back to our slides and Try and draw a picture about what's going on in here So here's a visual representation of what we've got going on. It's a class. It's called a class diagram Maybe you saw these in a previous class But it's a way of thinking of classes and instances of classes visually So x in our code is an instance of the class student and the class student has defined on it a method called say name y is an instance of the class another student and it also has a method in it called say name all right so Let's Run it again, but let's change it up just a tiny little bit. Okay, so We're going to take our code and we're going to reuse or we're going to reassign the variable x okay So let's go over here x gets another student and then x dot say name, right student variables These variables for your custom data type. They can be reused. I ignore that stuff down there at the bottom um My name is alice. My name is lucas right from our previous lectures remember Variables just hold references to objects in memory to instances of classes that live in memory so here x is a student and we call say name here I reassign the value I create another student instance Assign it to x and then call x say name. It all works out just fine Okay Go play with this in the python tutor plug this into the live visualization and step through it item by item So you can see what happens, right All right, let's go back to our slideshow again Whoo instances classes objects. Oh my, you know, these are They're not the most exciting words on the planet But they have very different meanings and it's important that you concretize in your head what those different meanings are Classes think of them as the blueprint for the data type. Okay They're kind of like function definitions The function doesn't like run right away You have to call it to get a function that you define to do something right go define a function in python It doesn't actually get used until you call it same thing with classes Classes are the blueprint of a new data type And they define its operations and and data But the class definition itself with that class keyword It runs but it's not like really going to do anything until you use it somehow instances of classes right when we call this Something that looks like this this constructor call They actually create An instance of that class in memory Python talks to the heap and says hey, I need one of these things. Where should I put it? And then it puts it there Right, so you need to get an instance of a data type an instance of a class before you use it to compute to do stuff, right? Okay, so classes instances of a class And then objects, okay Objects are the term for all instances in memory in python Everything in python Is an object everything that lives in memory in python is an object python is an object oriented language, okay May have heard that term before java is also an object oriented language Just means that in inside itself python is organized Everything in memory is organized around this notion of objects Okay, here's some code All right, look at this Just take a look at the code. Where is the class? Here Okay The class is this whole thing this whole block is the class, but again, it's just a blueprint When you run code if all you have is this you don't get any output You're not doing anything if you just have this part. It's a class definition Just like you have had function definitions before they tell python what to do X and y down here They take on instances. They are references to an instance of class student Okay, an instance of this guy But they're actually different This is something different. It's an instance of what? Z is an instance of class string class instances blueprint using the things All of them every single one of them are objects in memory Okay, because it turns out whenever python reads this thing It creates an object in memory and then any time you want one of these guys A student it goes out kind of grabs that that that definition that you put up there in memory and uses it Okay, so all of these things are objects in memory Objects are the generic term for stuff that lives in memory in python Okay Let's go back to methods for a second because they're they can be flexible All right, so let's say that instead of hard coding in my name is we want to pass Some data to our method Uh as a parameter. Well, you can do that Right, um, let's go edit our code so that our code can take a parameter all right so I'm back here my code. Let me get rid of The another student and so I get getting rid of another student. Let's start back where we were Well instead of hard coding. My name is lucas. Let's make this a parameter. Okay My name is And So I want to do something like say x dot say name lucas, okay All right. Well, what I will do is I will add a parameter up here And in my print statement, I will use This parameter. Okay. This is just like defining a function That takes a parameter except except obviously the huge like elephant in the room here difference is the self guy So the self guy is gonna stay there for now. Okay, we're gonna talk about him in a minute Let's explain what he does. All right, but I can run this. Okay. Go ahead and run this Okay Print my name is name. I've called say name with lucas. I can call say name with alice I can call say name with bob Just duplicating the line here with control d or command d Right, so now my method that I've created it can take a parameter Right and based on the argument value. I give lucas alice bob it prints out something different. Okay, this looks a lot like a function definition Okay, pretty cool If I wanted to you could add you can absolutely have multiple parameters Okay, so if I try and I can add last name here now Python's mad at me, right? I added a parameter up here, but I'm not using it in this method call Say name is missing one required positional argument last right yelling at me because I forgot to put Everything that I need that this function needs to run or excuse me. This method needs to run in the calls All right, there it goes. It's a lot happier now All right, so you can absolutely have methods That take parameters makes them a little more flexible, right? You can do a little bit more with something like that All right, let's get back here So we've had this guy staring us in the face go back to the code for just a second called self Huh, what in the heck is this self thing? What in the world is self? All right Okay self it's a keyword in python Self is a special variable That is only usable within a class that refers to the current instance of the class who It's how a class can access other methods of the class or other class instance variables Okay, let's take a look at this All right understanding self here's here's my student class All right, x gets student x dot say name lucas y gets student y dot say name bob There are two parameters to say name right there's self And then there's a name. This is the old version of say name Okay, but there's only one down here when we're calling this method All right, so what is happening? What is self? Okay, go back to the definition for a second Self is a special variable that only is usable within a class that refers to the current instance of the class Okay x dot say name So what happens with self here? You've got this x x is an instance of student You are calling in this line Say name on this particular instance of the student Self is the current instance Okay, so when you are calling x dot say name, basically what is happening is this x right here Becomes self Okay, let that sink in The dot operator here when we're using the dot operator The thing to the left of the dot Becomes the self Okay So then down here, we've got y is getting a different instance of student Go if you don't believe me go in use your id function use your hex function You will see that x has a different id than y. They are different things When we call y dot say name Self becomes y Okay So we're calling the say name method on two separate instances of student x and y Go look at this in the python tutor as well Okay Actually, maybe I can do that for you. Let's see if we can fire that up real quick. I'm going to copy this code Go into the python tutor start visualizing Okay I want to go into live programming mode because I want to see this thing evolve. How's that? All right It's not too bad. Let's Let's go to the first step and then I'm going to step through. Okay We're going to define our student now kind of jumped over this whole thing But the definition of what a student is is alive. Okay, let's step over here x Get student. We've got a new student instance All right I called say name. So what does it do just like in a function call? It's going to jump up to this definition of say name What do you take a real close look over here in the python tutor? Look what it has done in our Say name that we're inside of right now. What does self refer to? The same student instance that x refers to Okay, name is the the parameter value, which is currently lucas All right There's no return value. We print you see that print it out up here All right, we jump back we go to the next line Why get student? Okay, why is pointing to a separate instance of student? We call y dot say name It jumps into the definition of say name, but look Why is this guy self? Has been substituted right here for y thing on the left side of the dot operator. Okay So this say name is in reference to y whereas before say name was in reference to x Okay It's probably a little bit confusing Go back run through this until it starts to sink in self Is the thing on the left of the dot operator? Okay? all right So let me go back over here Whew self that's a crazy concept. Okay. It's it's going to be hard It's it's not going to take a little bit to sink in hopefully in the next video We're going to talk about instance variables class instance variables. It's going to make a little bit more sense about Why we need this self thing right right now this example Maybe it's a little extra. Maybe it's more than we need right next Video when we're talking about data that we're going to store in these data types self is going to be really important. Okay All right a lot of huge concepts in this video You can define your own data types with classes in python The operations of data types are defined using methods and data are called instance variables That's going to be the next video. Okay, you must create an instance of a class to use it And the instances that are in memory that live in the heap everything in python Those are objects all together You can have instances of lots of different things at once in memory You can have students and ints and floats and strings and lists all living together Those are all called objects in memory Methods are functions that are defined within a class methods can have parameters But the first parameter is always always always this self guy And what is the self the self is a special variable Inside of classes that refer to the current instance. Okay the thing on the left of the dot operator when they get called All right next video. We're going to take a look at instance variables inside of classes And we'll come back to this self guy again and try and understand why we need him and why he's so useful You