Hello, and welcome to Object Oriented Programming with your
host, Kalob Taulien
In this lesson, we're going to be talking about class methods.
Now, a method is honestly, it's just a function inside of a class.
That's literally all it is.
But because it comes with a few extra goodies, like being
able to access the 'self' keyword, it is slightly different
than a regular function,
and so we call it a method.
So I'm going to create a sample class here called 'MyClass'.
It's going to have an attribute,
a default attribute, 'course = "Python for Everybody",
and 'def __init__(self)'
I'm not going to use that, not just yet.
So technically, I didn't need to write that,
but because I did, I put 'pass' in there to basically tell
Python, "Hey, I know it's there.
There's nothing in there.
Just let it be".
And then I'm going to create another method in here, and
this is going to be a completely custom one.
And this is just going to be called 'thing'.
And so its first parameter is going to be 'self'.
First parameter is always going to be 'self', and that's what
connects it to this entire class.
And then it can take an optional parameter, if you want to
give it a parameter.
So I'm going to call this 'param1', and by default it's going
to have a regular value of "Thing", and all this is going to
do is 'print(thing)'.
That's all it's going to do.
So if you look at this, objectively speaking, this looks
exactly like a function.
The only difference is we have to put in 'self'.
So let's go ahead and run this.
And if you were screaming at your screen saying, "Hey, Kalob,
that's almost like a function,
except you missed a thing.
Yep, I did. I missed the colon.
So let's go ahead and run that.
And let's just instantiate this in a variable.
'my_class = MyClass()'.
So we actually run the class.
Nothing happens.
It looks like nothing happens.
But if we do 'my_class.thing', well, this is actually looking
for a 'param1', but by default, it's already going to be "Thing".
So it's going to 'print(thing)' in here,
and I actually did that wrong.
That's not supposed to be 'thing'.
I was looking at that while typing the wrong thing.
So let's rerun these.
There we go. It says "Thing".
But if I said 'my_class.thing("Something else")', and I overwrote
that parameter, it's going to overwrite the variable inside
of the method, inside of the function.
Now, where '__init__' comes into play, is we want to set a different
parameter in here.
So let's add the number of students in this class.
And so we're going to say, 'self.students = students'.
And if we create another method in here, 'def get_total_students',
pass in 'self',
we can 'print(self.students)'.
And so what this is doing, is saying when you create this
new class, put in the number of students.
So let's put 100_000.
And so this parameter is going to go into the 'students' variable
here, it's then going to be reassigned to 'self.students',
which is the same as saying 'students = 100_000'.
And because this is an attribute, it's a variable inside
of this particular class,
we have access to 'self.students' in here, and you can actually
see that it just makes that quick connection between
'self.students' here and 'self.students' here.
Just get rid of this one because we don't need that.
And when I run this
[no audio]
'Thing', still works.
That still works.
And if I do 'my_class.get_total_students',
this is going to print out the total number of students. Cool.
And in fact, a better example of this would be using
an f-string. We could say, 'f"The total students is ", we can put this
as a variable.
Let's save that. Rerun these.
And now it says, "The total student is: 100000".
Now the reason I did that, is because we can also now use
'self.students' from 'MyClass', or the variable.
So now we're jumping around from point to point.
We're really referencing a lot here, but we can now easily
say 'my_class.',
what was that variable name? Just 'students'.
[no audio]
And that gives me just a 100000, because that's
what I put up here.
So whenever you are using methods or functions inside of a
class, it looks exactly like a function.
So 'def', your function name, always take 'self',
and that's what's going to connect it to the rest of the
class, that gives it access to run 'self.thing'
if we wanted to, or get 'self.course', or 'self.students', that's
what connects it to this class.
And then parameter.
If we wanted to work with a parameter, we could force it
to be in there, and then just do something in here, any sort
of logic, it could print something, it could return something,
it could even change 'students'.
And actually, in fact, now that I think about it, that is a very good idea.
Let's go ahead, and update this with 'def add_student'.
It always takes 'self'.
And we're going to say 'self.students = self.students + 1'.
And let's print this out as, "New total is : ".
[no audio]
Let's run that, run that, run that, run
that, just rerunning all my old cells here.
'total_students', we're currently at 100000.
But if we do 'my_class.', and then hit 'Tab', we can see
we can add a student, 'add_student' in here.
That's a method,
so it takes parentheses.
And again, it does not need to have 'self' in there.
'self' only references that part, the class itself,
and Python magically does that for us behind the scenes.
So run 'my_class.add_student', and we could run this over and
over and over again.
And this will continue to add the number of students for us.
And then, if we just wanted to double check, we could do
'my_class.students',
and this is going to show us 100006 students, just like that.
[no audio]