This lesson will be a primer to the concepts of classes and objects (class instances) in the Python programming language.
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
Now, before we call it quits on this course, I would feel somewhat amiss if I didn't take you down one final deep dark alley in the world of not necessarily Python, but really just software development in general really depending on the language that you're talking about. But it's a rather deep topic, and that's called classes. Now if you've ever worked with classes before this will be probably a fairly simple adjustment for you if you've done it before, maybe not necessarily in Python. But it's gonna be a common topic that you're probably is going to be comfortable with. If you are not familiar with or comfortable with classes. That's okay, we're gonna walk you through the absolute basics here now. Do understand that classes, and then the things that build on top of classes, things like talking about object oriented programming, and then on down the line, are very deep topics. Entire books and courses in school, and all of those types of things have been created to just talk about this specific topic. So, there's no way I'm gonna cover everything in this course but I just want to give you a little bit of an introduction, so that you're at least armed with this tool and with this nomenclature in the terminology, so that you can go out and have educated conversations about this with your fellow developers, and be able to maybe better read their code, and write your own code to share with other people and to at least understand these basic concepts. So, before we can actually start creating a class it really would benefit us to know what a class actually is. And a class is nothing more than a blueprint. A blueprint that's going to describe the features and functionality of some sort of object. Now, those are two very distinct and very important terms to understand when you're talking about classes. And ultimately object oriented programming is the concept in the difference between a class and an object. So once again, a class is nothing more than a blueprint. It's going to describe the functionality in and of itself a class doesn't actually do anything other than describe features and functionality. Whereas, when you create a new instance of a class, or create an object of that particular type, then that particular object has all of those features and functionality and can use them. So, let's go ahead and see what that actually looks like within Python. So, let's say I wanted to create a class. I'm going to use the class keyword in a very similar fashion to how I used the d, e, f, keyword when I was creating functions. I'm now going to say class and I'm going to specify a name of a class. So, I'm going to say we're going to call this person. Now, this can be any name that you want. But just remember, you want to name it logically after what it is you're trying to model, or what it is you're trying to blueprint. So, I'm going to create a general class called person. And I'm going to end that declaration with a colon just like everything else. And I'm gonna drop down one line, and I'm going to indent. So all the normal, Pythonisms are going on here, we have our colon and we have our indenting. Now, we can start to define all of the different characteristics of a person. Now, there's generally two different groups of functionality that get put into a class within Python. And they're called attributes and methods, and we're gonna talk about those a little bit more in an upcoming lesson. But just to kind of see how this is going to work. I'm going to throw a little something at you that you're not gonna understand right away if you've never seen it before, and that's okay don't worry about it. It's just a little bit of necessary cruft if you would like and some boilerplate. And then, I'm gonna throw a little something in there so you can see how this is going to work. And then, ultimately, you'll be able to piece things together and you'll start to see how these things work, and then you'll be able to write them on your own, and then we'll discuss them a little bit more, so that they make a little bit more sense. So, the first thing that we're going to need to do is we're going to need to define how we are going to create this person and maybe define some of these properties or attributes as they're called in Python. And the way that we do that is typically by a special function, it's a function, really, is what it boils down to. And it's going to take in at least one parameter, named really whatever you'd like, but the generally accepted practice here is to call it self. And we'll talk about why that is in the next lesson. So now, we're going to drop down a line and in here now is where we're going to specify the attributes that are going to be associated with a person. So, we start to think of a person from a generalized standpoint when you start to talk about a blueprint of a person what are some characteristics, or some attributes that maybe every single person or most people will have? And I would say. I could boil that down to a couple. Maybe a first name ,a last name, and an age. So, we can do something like self.first is going to be equal to, and we'll just give it my name for now, Derek. And we'll say self.last is going to be equal to Jensen, and it'll say self.age is going to be equal to 30. So, really all we've done here is we've used this kind of specialized initialization function, or constructor, if you will, if you've ever heard that name before in the past and other programming languages. And then within there we have defined a few what are known as attributes. And in this case, specifically, they are called instance attributes, but we'll talk about that in the next lesson. So now, we've created this class, this blueprint, that has a couple different properties on it first, last, and age. And then, let's say we want this person to be able to give some sort of a greeting. So, that's kind of a generalized thing that maybe every single person should be able to do, we may not all be able to do it very well, but that's definitely a piece of functionality that all people should ultimately have. So, let's go ahead and create a simple function here. And we're gonna call this greet, and we will once again pass in self. And we will once again discuss that in the next lesson as to why we're doing that and then once we've done that we're simply going to return hello there. So, we've seen something very similar to this in the past where we were working with a very simple function but in this case we're going to call this a method. And now, we're going to return hello there, just like we've done in the past. And I'm gonna save this and I'm gonna save this as person.ply, like that. And now, I want to go ahead and use this. So now that I've created this class I've done the blueprinting. Now, you can obviously get much more detailed and add a lot more definition of functionality here within my class, within my blueprint. But then I'll leave that kind of up to you to play around with. Now, I actually want to create a person. I want to create a new object of type person. How do I do that? Well, I'm gonna go into my interpreter. And I'm going to import. I'm gonna say actually, from person I want to import my person class, cuz that's all I really want to get access to. So now, I can take a look at person and sure enough there it is, class person.person. So I have access to it now. So, how do I create a new person object? How do we create this instance that we're talking about? Well it's quite simple. We're going to give it a variable name so we'll say me, or whatever name you'd like. This is going to be equal to, and we're going to specify the name of our class. So, right here we see person. That's gonna be the name. And then, we're going to append onto the end of this open and close parentheses. So, you're basically looking at this as I want to specify the name of my class. And in this particular case, it's going to take in no arguments even though we kind of see we have this constructor this initializer here with self, but we'll get to that later. So right now, it says me=person() so let's go ahead and hit enter. Well, it looks like it works but how do we really know. So, I could type in me and we see that sure enough it is a person object. Now, let's see if we can get access to some of these properties that we've had here before like first last and age. So, I could say me.first I get Derrick, me.last I get Jensen, and me.age I get 30. Well, that all seems to work just fine. Now, what about this greet method here? Well, I'm going to type in me.greet. And I need to remember to put in my parentheses. And it's going to say hello there. So, everything we have done here seems to work. We have done things In a very generalized way we've created a Person class. Remember, that is a blueprint that's going to describe the features and functionality of a general person. And in here we're a little bit restrictive. We've created this constructor and this basically means that every person in the world or every person in our application that we create is Derek Jensen, and I mean how bad could that possibly be. And then, all of these instances of Derek Jensen, are capable of greeting others with a very simple method here called greet. And it's going to return Hello there. So, we really kind of boiled all of this crazy class talk down into some very simple concepts. We've created a class that contains a couple different functions that we're gonna call methods. Because really at the end of the day, the only difference between a function and a method is just this fact that a method is a function that lives within a class. So, really functions and methods you'll hear a lot of people use the terms interchangeably and that's fine, but typically, generally speaking, a method is a specialized function that lives within a class. So, that's pretty much it right there. So now, that you've got a grasp on the concept of a class and the difference between a class and an object. In the next lesson, we're going to talk a little bit more specifically about some of the details around these attributes that we're working with and how to define them in some different ways.