- [Joe] Just like other languages, such as JavaScript,
Python gives you the option of writing code
that is procedural or object oriented,
so 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.
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 method1
and I have another one called method2.
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 the, 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 = myClass with parentheses.
And that will instantiate an object instance of this class.
So once I got the class created,
I can then call them methods on 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 runtime,
I just have to care about the arguments
that I'm passing into each of the methods.
Let's save this and let's run it.
So I'll 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 method1,
we're printing out myClass method1, and that's this result.
And then myClass method2, plus whatever the argument
that was passed in.
So that's this, 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.
I'm just gonna copy this one
and paste it and I'm gonna rename it anotherClass.
And this time it's going to inherit from myClass,
so I'm gonna put myClass inside the parentheses right here.
And then inside method1,
which just takes a self keyword,
I'll print anotherClass method1.
And then in method2,
I'll print anotherClass method2,
but I won't use the argument in this case.
All right, so now I've got two classes.
The first is myClass, which you've already seen
and then anotherClass, 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 = anotherClass,
and then I'll call c2.method1, so we'll save.
So now I'm creating c2,
which is an instance of anotherClass,
and then I'm gonna 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 method1 for anotherClass.
I'm going to write myClass.method1.
I'm inside the class now,
so now I do need to pass the self around when I use it.
All right, 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 method1,
myClass method2 This is a string.
In addition, the new class's method1 right here
is being called and it's printing out myClass method1
because we're invoking that function right here,
and then it's printing out anotherClass method1,
which is this line right here.
So let's go back into the code
and add a function call
to c2.method2.
And once again, I'll pass in, This is a string and save.
Now you can see that in anotherClass,
method2 is not calling the base class,
it's not calling the function
that's up here in myClass, right?
It simply overrides the existing method2
that's in the base class, but does its own thing.
So now, scroll back down and let's save and let's run.
And you can see anotherClass's method2 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.
All right, 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.
(upbeat music)