Hey, welcome back.
In this video, you learn about the difference between an instance method and a class methods.
We did cover instance methods so far.
So all these here are instance methods.
Syntactically, you can tell that something is an instance method.
When you see that self is the first parameter.
All of that methods logically what makes an instance method and instance method is that the method?
Can be applied to an instance, Right.
So we can check here, for example, hotel one that available.
So perhaps print out its output.
Let me remove this last line, because as you saw in the previous video, that was to show you that
that line produced an error.
So now we should see true print it out out of available.
So the method is an instance method because it can be applied to hotel one and it can also be applied
to Hotel two.
So an instance method is very tightly associated with its instance.
So you are checking if this particular instance, this particular hotel.
Whose name is.
Snow palace.
If this particular hotel is.
Available or not.
The same goes with other methods such as book.
You are booking that particular hotel.
Same goes with reservation tickets.
You are generating a particular instance of the reservation tickets.
You are printing that out.
But what is a class method then?
And when would you need a class method?
So let's create a class methods here.
To create a class method, you use the diff keyword.
Again, pick a name for the class methods such as hotel count.
Get hotel count.
Now instead of self with class methods, you write something like C.
Else I'll cover in just a bit what classes.
And then perhaps other arguments such as DF or data.
And then hear Return.
Len.
So.
I just created a class method which will return the length of the data frame, which means that, for
example, for this particular data frame we will get three.
This will return three, right?
Because this does a frame has three rows.
And lastly, to finalize this method, you should add the class method decorator just above the function.
So don't leave any brake lines here.
This is a recommended style, so that decorator will basically change this method to a class method.
Now class, this can also be self write because it's just a variable.
But to be in line with best practices, you should use seals here because this actually will be a variable
which will hold the class object, not the instance object.
So everything in Python is an object, right?
So when you go to a Python console, for example, and you do class A.
Let me pass just a simple class.
A is an object, but a.
The instance of A is also an object.
So that means.
In class.
This variable will hold the class.
Now, why did I add this method to you?
Well, this method, as you'll see.
So let me call it here.
Print Hotmail dot.
Get.
Hotel count.
Underscore hotel count with parentheses.
And it needs a.
An argument.
The data argument will be equal to d f our data frame which is assigned here as a global variable and
run.
And we get three printed out.
So notice that I used hotel the class.
Here boats.
You can also access a class variable using an instance.
So Hotel one is an instance I've created up here, and if I run this, we'll get to threes printed out.
One for that and one for that.
Now, you might ask, well, shouldn't this method also be an instance method?
Well, not because it's this is not related to a particular instance.
Right.
This is related to the class somehow.
So it's about hotels, but it's not related to a particular hotel.
It's not related either to tourist sunny apartment nor to snow palace or city break in.
So whenever you need a method which is somehow related to the class but not related to an instance,
then you want to create a class methods and you use that class method decorator and that seals parameter.
And that's about class methods.
I hope that makes sense and I'll talk to you in the next video.
Thanks.