1
00:00:00,940 --> 00:00:01,930
Hey, welcome back.

2
00:00:01,960 --> 00:00:07,260
In this video, you learn about the difference between an instance method and a class methods.

3
00:00:07,270 --> 00:00:10,120
We did cover instance methods so far.

4
00:00:10,150 --> 00:00:13,730
So all these here are instance methods.

5
00:00:13,750 --> 00:00:17,590
Syntactically, you can tell that something is an instance method.

6
00:00:17,590 --> 00:00:22,200
When you see that self is the first parameter.

7
00:00:22,210 --> 00:00:30,610
All of that methods logically what makes an instance method and instance method is that the method?

8
00:00:31,450 --> 00:00:35,560
Can be applied to an instance, Right.

9
00:00:35,560 --> 00:00:44,350
So we can check here, for example, hotel one that available.

10
00:00:44,800 --> 00:00:47,860
So perhaps print out its output.

11
00:00:50,290 --> 00:00:55,330
Let me remove this last line, because as you saw in the previous video, that was to show you that

12
00:00:55,480 --> 00:00:57,850
that line produced an error.

13
00:00:58,240 --> 00:01:04,450
So now we should see true print it out out of available.

14
00:01:04,780 --> 00:01:10,390
So the method is an instance method because it can be applied to hotel one and it can also be applied

15
00:01:10,390 --> 00:01:11,410
to Hotel two.

16
00:01:11,440 --> 00:01:17,350
So an instance method is very tightly associated with its instance.

17
00:01:17,560 --> 00:01:23,050
So you are checking if this particular instance, this particular hotel.

18
00:01:24,200 --> 00:01:25,910
Whose name is.

19
00:01:27,140 --> 00:01:28,760
Snow palace.

20
00:01:29,780 --> 00:01:32,570
If this particular hotel is.

21
00:01:34,040 --> 00:01:36,170
Available or not.

22
00:01:36,950 --> 00:01:40,050
The same goes with other methods such as book.

23
00:01:40,070 --> 00:01:42,770
You are booking that particular hotel.

24
00:01:43,550 --> 00:01:46,310
Same goes with reservation tickets.

25
00:01:46,550 --> 00:01:52,010
You are generating a particular instance of the reservation tickets.

26
00:01:52,010 --> 00:01:53,450
You are printing that out.

27
00:01:54,470 --> 00:01:57,200
But what is a class method then?

28
00:01:59,270 --> 00:02:01,840
And when would you need a class method?

29
00:02:01,850 --> 00:02:04,790
So let's create a class methods here.

30
00:02:05,840 --> 00:02:09,800
To create a class method, you use the diff keyword.

31
00:02:09,800 --> 00:02:15,860
Again, pick a name for the class methods such as hotel count.

32
00:02:16,190 --> 00:02:17,630
Get hotel count.

33
00:02:18,630 --> 00:02:24,700
Now instead of self with class methods, you write something like C.

34
00:02:24,720 --> 00:02:29,220
Else I'll cover in just a bit what classes.

35
00:02:29,640 --> 00:02:35,130
And then perhaps other arguments such as DF or data.

36
00:02:38,140 --> 00:02:39,940
And then hear Return.

37
00:02:40,240 --> 00:02:40,860
Len.

38
00:02:41,590 --> 00:02:42,310
So.

39
00:02:43,630 --> 00:02:54,400
I just created a class method which will return the length of the data frame, which means that, for

40
00:02:54,400 --> 00:02:57,720
example, for this particular data frame we will get three.

41
00:02:57,730 --> 00:03:00,040
This will return three, right?

42
00:03:00,040 --> 00:03:03,370
Because this does a frame has three rows.

43
00:03:03,580 --> 00:03:15,340
And lastly, to finalize this method, you should add the class method decorator just above the function.

44
00:03:15,340 --> 00:03:17,650
So don't leave any brake lines here.

45
00:03:17,680 --> 00:03:28,090
This is a recommended style, so that decorator will basically change this method to a class method.

46
00:03:28,120 --> 00:03:33,220
Now class, this can also be self write because it's just a variable.

47
00:03:33,640 --> 00:03:43,720
But to be in line with best practices, you should use seals here because this actually will be a variable

48
00:03:43,720 --> 00:03:48,400
which will hold the class object, not the instance object.

49
00:03:48,400 --> 00:03:51,290
So everything in Python is an object, right?

50
00:03:51,310 --> 00:03:57,550
So when you go to a Python console, for example, and you do class A.

51
00:03:59,100 --> 00:04:01,230
Let me pass just a simple class.

52
00:04:01,650 --> 00:04:05,940
A is an object, but a.

53
00:04:07,270 --> 00:04:09,790
The instance of A is also an object.

54
00:04:10,700 --> 00:04:11,900
So that means.

55
00:04:13,410 --> 00:04:14,640
In class.

56
00:04:15,460 --> 00:04:18,010
This variable will hold the class.

57
00:04:18,040 --> 00:04:20,649
Now, why did I add this method to you?

58
00:04:20,680 --> 00:04:23,210
Well, this method, as you'll see.

59
00:04:23,230 --> 00:04:25,000
So let me call it here.

60
00:04:25,000 --> 00:04:27,490
Print Hotmail dot.

61
00:04:28,240 --> 00:04:29,080
Get.

62
00:04:31,920 --> 00:04:33,120
Hotel count.

63
00:04:33,150 --> 00:04:38,580
Underscore hotel count with parentheses.

64
00:04:39,240 --> 00:04:41,010
And it needs a.

65
00:04:41,610 --> 00:04:42,600
An argument.

66
00:04:42,600 --> 00:04:49,710
The data argument will be equal to d f our data frame which is assigned here as a global variable and

67
00:04:49,710 --> 00:04:50,310
run.

68
00:04:52,150 --> 00:04:54,100
And we get three printed out.

69
00:04:54,550 --> 00:04:58,270
So notice that I used hotel the class.

70
00:04:59,230 --> 00:05:00,460
Here boats.

71
00:05:00,460 --> 00:05:06,730
You can also access a class variable using an instance.

72
00:05:06,730 --> 00:05:15,190
So Hotel one is an instance I've created up here, and if I run this, we'll get to threes printed out.

73
00:05:16,450 --> 00:05:19,030
One for that and one for that.

74
00:05:19,060 --> 00:05:23,980
Now, you might ask, well, shouldn't this method also be an instance method?

75
00:05:24,490 --> 00:05:29,140
Well, not because it's this is not related to a particular instance.

76
00:05:29,830 --> 00:05:30,340
Right.

77
00:05:30,340 --> 00:05:33,420
This is related to the class somehow.

78
00:05:33,430 --> 00:05:38,920
So it's about hotels, but it's not related to a particular hotel.

79
00:05:39,190 --> 00:05:45,670
It's not related either to tourist sunny apartment nor to snow palace or city break in.

80
00:05:46,740 --> 00:05:54,450
So whenever you need a method which is somehow related to the class but not related to an instance,

81
00:05:54,450 --> 00:06:04,800
then you want to create a class methods and you use that class method decorator and that seals parameter.

82
00:06:05,010 --> 00:06:06,740
And that's about class methods.

83
00:06:06,750 --> 00:06:09,750
I hope that makes sense and I'll talk to you in the next video.

84
00:06:09,750 --> 00:06:10,170
Thanks.

