- [Presenter] Just like other languages, such as JavaScript, Python gives you the option of writing code that is procedural or objected oriented. So, that you can use classes in Python. Classes are a really great way of encapsulating functionality that can be kept together and passed around as a complete module for use in other projects and that's what we're going to take a look at now. So, let's open up classes underscore start, here in chapter two. You can see I've got the beginning of a main function, here but, I'm not going to add code to the main function, just yet.
I'm going to start by defining a class. So, let me just add some code, here and I'll define some methods and I'll explain all of this in a moment and this one will take an argument. Classes are defined using the class keyword and they're given a name and if this class was based on a superclass that I was inheriting from, I would put the name of that other class, here inside these parentheses.
But this is just a simple base class that doesn't depend on anything else, so, I don't have to put anything in there. Inside a class, I can define functions or in object oriented terminology, they are called methods and they are part of this class. So, I indent my code and then I use the def keyword just like I would for any other function and in this case, I'm calling it method one and I have another one called method two. Usually, the first argument to any of the methods of a class, is the self argument and the self argument refers to the object itself.
It's kind of like this keyword in JavaScript. So, inside this method, self will refer to this particular instance of the object that's being operated on. So, down here, in the main function, I'll instantiate the class by writing C equals myClass with parentheses and that will instantiate an object instance of this class. So, once I've got the class created, I can then call methods in the class like I can call methods on any other object.
So, I'll write C.method1 and then I can write C.method2 and I'll give it an argument. This is a string. Now, notice that when I call the class methods I don't have to supply the self keyword. That's automatically handled for me by the Python run time. I just have to care about the arguments that I'm passing in to each of the methods. Let's save then and let's run it. So, go to my debug view and remember, you can run this in your terminal if you're not using VS code.
So, I'll click run and I'll show the output window and I'll run it and you can see here, inside method one, we're printing out myclass method one and that's this result and then, myclass method two plus whatever the argument that was passed in. So, that's this is a string, right here. That's the output that we're getting. Now, let's take a look at another feature of object oriented programming. This time, we're going to create another class. So, let's do that. So, I copy this one and paste it and I'm going to rename it another class and this time it's going to inherit from myclass.
So, I'm going to put myClass inside the parentheses, right here and then, inside method one, which just takes self keyword, I'll print another class method one and then, in method two, I'll print another class method two but I won't use the argument in this case. So, now, I've got two classes. The first is myClass, which we've already seen and then, another class which is based on the first one, based on myClass.
Let's go ahead and take a look at exercising this. So, now, I'll write C2 equals another class and then, I'll call C2.method1. So, we'll save. Now, I'm creating C2, which is in instance of another class and then, I'm going to call the method, which is right here. The first thing I'm going to do is call the inherited method on the superclass, which is this method, here in myClass and the way that I'm going to do that is in the method one for another class, I'm going to write myClass.method1.
I'm inside the class one. So, now I do need to pass the self around when I use it. So, let's go ahead and save and let's run it. You can see that the code we were originally using is still there. So, here's myClass method one, myClass method two. This is a string. In addition, the new classes method one, right here is being called and it's printing out myClass method one because we're invoking that function, right here and then, it's printing out another class method one, which is this line, right here.
So, let's go back into the code and add a function called C2.method2 and once again, I'll pass in this is a string and save. Now, you can see that in another class, method two is not calling the base class. It's not calling the function that's up here in myClass. It simply overrides the existing method two that's in the base class but does it's own thing.
So, now, let's scroll back down and let's save and let's run and you can see, another classes method two is being called. Even though we're passing in an argument, the argument is not being used. So, it's not being printed and since we're not calling the inherited method, the argument is not being printed out in the myClass code. So, that's a quick introduction to object oriented Python and as you can see, you can define classes in Python as well as classes that inherit from other classes.
Updated
10/7/2020Released
1/30/2018- Installing Python
- Choosing an editor or IDE
- Working with variables and expressions
- Writing loops
- Using the date, time, and datetime classes
- Reading and writing files
- Fetching internet data
- Parsing and processing HTML
Share this video
Embed this video
Video: Classes