As well as supporting different types of attributes, Python can also have different types of methods. Let's take a look!
1.Introduction2 lessons, 11:32
2.Python Building Blocks6 lessons, 1:08:07
3.Controlling the Flow7 lessons, 1:20:10
4.Common Data Structures4 lessons, 46:49
5.Application Structure7 lessons, 1:15:12
6.Collections7 lessons, 46:55
7.File I/O6 lessons, 48:51
8.Networking5 lessons, 43:48
9.Connecting to Network Services3 lessons, 34:27
10.Conclusion1 lesson, 02:08
So in the last lesson we gave some attention to the attributes. In the way of instance and class versions were gonna do the same thing in this lesson for the world of methods. Now methods kind of work in a similar way. We do have a couple different categories and we kind of have a middle of the road category of different types of methods. We have our instance methods which are these here, and by the same token they are defined as instance methods because they are receiving this instance as the first parameter, this self here. Then we also have a couple different kinds. We have the class method, and then we also have kind of the in-between known as the static method. So now let's go ahead and take a look at these couple different variations. Now before we created this. These two different methods. And recall method is nothing more than another way to refer to a function, but a method is a function that lives within the realm of a class. So technically speaking You could kind of walk interchanging those words and you're going to hear a lot of people using those words interchangeably. You're just gonna have to let it go even though, technically speaking, they are slightly different by definition. Once again because a method is a function that lives within a class. But like I said they're gonna get used interchangeably so you're just gonna have to get used to it. So when we're creating this first method here, this constructor, this initialization method. We're passing in this one parameter, and then we're defining all people that get created this way as being Derrick Jensen. And while that might not be a horrible thing, it might be a little inflexible. So let's say I wanted to customize this a little bit, to be more reflective of the real world. So maybe I wanted to pass in those Arguments as the first name the last name and the age. So I could make all the different instances of this class unique. So I could give all the different instances it's own first name its own last name and then its own age like that. So let's go ahead and save that. And we'll go ahead and say from person want to import person. And I will create a new instance of me. And this time we're gonna set it equal to person. Open/close parentheses and we'll hit enter and it's going to bomb. Because it says it's missing the three required positional arguments, first, last, and age. Now that does seem a little funny. Because there are four here but remember we're always getting past in this first one which is an instance. The instance that we're creating in this case. By default we don't have to do anything, so we're only missing these last three. So let's go ahead and create this correctly. So now I need to pass in Derek for first, Jensen for last. And 30 for age. And I can say meet up first and all those wonderful things that I could take a look at last in age as well but I'll save you that. Now and I also have instance or I also have access to my Greek method as well. Just like I had in the past. So now let's say though I have a situation where I want to be able to create a function. or a method in this case, that is more generic. That does kind of live at the class level, that doesn't necessarily care about any of these instance attributes, in being able to access self or anything like that. It's a little bit more generic. It's a little bit higher than the instance living maybe at the class, but doesn't need access to those types of things how could I do that? Well, I could create another method here I would define a method and I would give it a name in this case we'll just call it Wave. And I'm not going to define any arguments for this particular method. So remember the way that I define an instance method is by providing this first parameter. Well, I'm not doing that here, so by default I'm already saying this is not going to be an instance method. So then we'll come down here. We'll simply return, waving hands feverishly. So this is the functionality that's gonna be by default for the class person. Not necessarily the instance of the class like me, like we've done in the past over here. Now technically speaking this is a valid way to define this. But what you're typically going to see, is you're going to see what's known as a decorator applied to this. And a decorator in Python, we're not going to get too into that. We're going to save that in the more In-depth look at classes and other things like object oriented programming in an upcoming course. But a decorator in this case is going to be the and symbol and this is going to be called a static method. And all that means is it's not going to have access to any of the instance attributes. So let's go ahead and save that. So we'll go ahead and open this back up and now we'll from Person, we want to import person. Once again create a new instance of me. So we'll have person is going to be equal to Derek Jensen and 30. And now I can say me.first just like I've done before me.last, all that kind of good stuff. And now I also kind of have access to this new piece of functionality called wave. So I could say me.wave, and it's going to say waving hands feverishly. So the difference here, is that we're creating a method, a static method that is a method that we define at the class level. This is not an instance but it's going to be accessible to all of the instances that live or that are created from this class. But this method does not have access to any of these things here, to any of these instance attributes. Just to prove that, we could come down here, instead of doing this waving hands feverishly. We'll say return self.first, just because, we'll go ahead and save that. We'll get out of here and we'll do this one more time. We'll from person we want to import person. Me is going to be equal to person, Derrick Jensen and 30. And, now if I want to do me.wave, As you can see here, it doesn't have any access. It has no notion of what this self concept is, because we're not passing it in. So that kind of makes sense. So let's go ahead and we'll return. We'll say that, we'll cancel out of here. Now the final kind of characterization here that we want to talk about is the class method. And the class method, it's gonna be similar to an instance method in that it's going to take in a parameter or an argument, but it's not going to take in the instance of the class that we're working with. It's going to be a little bit different. So this case we're going to create another method, and we're going to call this say_foo. And we're not going to pass in nothing, and we're not going to pass in an instance of our class. We're going to pass in the class that we're working with. So we'll just call this cls for now. And then we'll go ahead and come down into our function. And then we're just going to return foo. Now this might Might seem a little strange but let's just kind of follow along with this train of thought. So let's go ahead and save that, but before we do, let's pop up here and because we're creating kind of a specialized method here, in a similar way that we did to wave, we want to use another decorator here in this case. Because this is kind of a special type, or a special method. And this particular decorator's going to be similar in that it's going to start with @ and this is going to be a class method. Because it's being passed in the class so it has access to this class. Let's go ahead and save that. Now also I want to come in here, and I want to modify this return and I want to return foo here as well. So let's go ahead and save all of this. Now let's come into Python, our interpreter, again. And hopefully, for one final time, we're gonna say from person. You want to import person. And then here we can create me again. So we'll say me = Person. Derek, Jensen, and 30. So now I have me.first. I've got all that kind of good stuff. I have access to this Wave, static method now. But the interesting thing here is that from within my static method I don't seem to have access to this class attribute. So that's kind of one of the other gotchas about a static method. So a static method really only can do its own logic if you will, it does not have access to the instance so it can't get a hold of any of these instance attributes. And it doesn't have any access to the things that the class level doesn't have access to any class attribute either. So the only things that a static method can work on are the arguments that are passed into it. So if you want to use a static method that's fine. But in order for it to do work on anything, everything has to get passed in. So that's kind of the definition of a static method. Now contrary to that, if I wanted to use a class method, a class method, once again, is not passed in an instance of our class. So it still does not have access to any of this self business. But It does have access to the class attributes that are working within this class. So if I were to come in here once again and I wanted to say me say foo, it doesn't seem to work. And why exactly is that? Because we have forgotten one very simple thing. If I were to come into my class method. Yes I do get access to foo, but not directly. This way I get access to foo through the class that is passed in so I have to refer to that class. And then refer to that class and tributes and I can say save. And then finally one more time we'll go into Python. We'll say I want to import from Person. Import Person. And we'll say me equal to Person. Derrick Jensen and 30, once again you can pass it anything you want there. And this time you can say all right I want to do wave. Once again, wave is not going to work but I could do me.say_foo. And now I'm gonna get access. Because I was passed in class, I get access to the class attributes. So now these are all a bunch of different variations on ways of creating and accessing ultimately attributes as well as methods. And you might be thinking yourself, well you know what good is all of this stuff? Well it just depends on the situation. You could be creating all sorts of different types of classes with different access levels and different things going on, and depending on how you're creating these things, these different types of methods may be necessary in certain cases. Now ultimately you're gonna have to play around with these a little bit to get a little bit more comfortable with them. But then eventually you'll start to understand them more. And when you start to see these things out in the wild in code snippets here and there, or maybe when as you write your own classes. You're gonna start to see how you can use these types of things. And they really come in handy when you start to do things like venture into the world of object oriented programming. And like I said, unfortunately that's a little bit too big of a topic for this particular course. So we're gonna save that for an upcoming Python course. But just kind of search around a little bit. If you were to look for object-oriented programming or OOP in Python, you'll see a lot of examples on how to do that. And some of the different characteristics that are typically defined when you're talking about object oriented programming. So that's pretty much gonna wrap up the basics or the introduction of classes for Python.