Welcome back.
We learned that we were able to create an actual attribute for the class.
But what about a method?
Is there a way to do something like here?
What we do with attributes but for methods?
Well, there is.
And we use a decorator, which is a topic that will cover later on and you'll understand how these work
underneath the hood in a bit.
But for now, the syntax that we're going to use is the add side.
And then we call a class method like this.
And now with this decorator, we can now write a function.
For example, let's create a function and we'll call it.
Adding things that will take number one and number two.
And in here.
Oh, make sure we have the define for the function and in here we'll just return num one.
Plus numb to.
Okay.
So what does this class method do?
Let me minimize this just so we can see a little bit better.
Now in here.
Let's see if player one has access to adding things.
So let's just say adding things to and three if I click run.
Takes two position arguments, but three were given.
Hmm.
That's on line 17.
So when we call this, we gave it two parameters.
But then it was expecting it says that we actually got three and that is because adding things.
The first parameter, just like we have here is.
The SEALs and SEALs stands for class.
So this player character, that's the class that we're referring to, so similar to what we've seen
before.
But the standard is to use seals.
Now, if I run this.
You'll see that we get five.
Och.
But looking at this, it looks like it's pretty much the same as the above.
Except instead of using self we use CLS.
But how is this a class method?
Well, it's because we can actually use this without even instantiating a class.
Let me show you.
Let's say that we don't instantiate player one and now I just simply call in here.
Player, character, dot adding things.
If I click Run.
Look at that.
I get five.
It's a method on the actual class.
It's a class method.
So you can do some interesting things.
And by the way, class methods aren't used as often.
So if you're thinking, why do we even need this?
Well, I would say 95% of your classes aren't going to use something like a class method, but there
are cases where this might be useful.
For example, we can use the CLS to actually instantiate an object in here.
So, for example, I can say that CLS.
Which is the class, which is the player character, and I'm going to instantiate it, remember, just
like we did here with the brackets with num one plus num two as the second parameter and we'll give
it name Teddy.
If I hit run here.
Look at that.
I've instantiated an object, Teddy, with the age of what should be five.
So let's.
Take this.
Make this equal to player three.
And if I do player three right here, dot H and I click Run.
I get five.
So I have a whole new player here that was created by using this class method.
So I was able to create just like I created a class object attribute, a class method using this at
sign.
There's also one other way of doing something like this, which is the static method.
And the static method works the exact same way as here.
Except.
You do not have access to this class or the class, so you can't do something like we did above.
Instead, we just perform some sort of method.
Like adding things to like this.
So the only difference between the two is the idea that we don't have access in our parameters to this
CLS.
So we would use something like static method where we don't care anything about the class state.
A class state is something like these.
We don't care about the attributes really.
We use something like a class method when we do care about these attributes and maybe we want to modify
them or change them.
Now these class method and static method is something that you won't see very often.
But I wanted to show to you just because you might encounter it through your Python career.
Hopefully this wasn't too confusing.
In the next video, I want to review what we've just learned up until now.