1
00:00:00,600 --> 00:00:01,560
Hey, welcome back.

2
00:00:01,589 --> 00:00:05,640
In this video, you learn how to create class variables.

3
00:00:05,640 --> 00:00:09,510
You have learned already how to create instance variables.

4
00:00:09,510 --> 00:00:12,880
So we have instance variables and we have class variables.

5
00:00:12,900 --> 00:00:18,570
I'll mention the difference between the two and show you what each of these are.

6
00:00:18,660 --> 00:00:27,480
We're going to work on the hotel booking app we've been building so far, but I'll simplify the main

7
00:00:27,720 --> 00:00:29,420
pie script a little bit.

8
00:00:29,430 --> 00:00:34,140
Actually, I'll just make a copy of this file, so please do the same.

9
00:00:34,470 --> 00:00:44,610
I'll name the second copy main to that P and then in main to that py, I'll simplify the code a little

10
00:00:44,610 --> 00:00:47,760
bit so I'll remove.

11
00:00:48,850 --> 00:00:53,350
The two classes, credit card and secure credit card.

12
00:00:54,490 --> 00:01:02,410
I'll remove the entire loop where we run the program, and I'll keep only the two classes, reservation

13
00:01:02,410 --> 00:01:04,450
ticket and hotel.

14
00:01:04,450 --> 00:01:09,520
And also these two lines are pointing to the cards that CSV file.

15
00:01:09,520 --> 00:01:11,560
So I'll remove those too.

16
00:01:11,590 --> 00:01:15,760
Now I'm also attaching this script to the lecture resources.

17
00:01:15,760 --> 00:01:21,010
So if you want to make sure that you have the same code as me, you can just copy that file and paste

18
00:01:21,010 --> 00:01:25,590
it in your project directory and name it with something like Main two PI.

19
00:01:25,810 --> 00:01:29,680
So instance variables versus class variables.

20
00:01:29,800 --> 00:01:33,800
You have already created instance variables.

21
00:01:33,820 --> 00:01:36,340
Instance variables are these.

22
00:01:37,530 --> 00:01:39,120
Hotel ID.

23
00:01:39,480 --> 00:01:47,530
Name for the hotel instances, customer name and hotel for the reservation.

24
00:01:47,550 --> 00:01:48,930
Ticket instances.

25
00:01:50,000 --> 00:01:56,750
Now let me create a class variable and then explain you the difference between a class variable and

26
00:01:56,750 --> 00:01:57,950
an instance variable.

27
00:01:58,430 --> 00:02:05,530
Class variables are coded outside the methods of a class instance.

28
00:02:05,540 --> 00:02:08,380
Variables are coded inside a method.

29
00:02:08,389 --> 00:02:10,520
Usually it's the init method.

30
00:02:11,190 --> 00:02:13,620
But class variables are coded here.

31
00:02:13,620 --> 00:02:23,610
For example, let's say watermark is equal to, let's say, the real estate company.

32
00:02:24,300 --> 00:02:28,860
This is the company that has this app, this hotel booking app.

33
00:02:28,860 --> 00:02:35,700
So they are a company who offer hotels for holidays for people who go to vacation and so on.

34
00:02:35,850 --> 00:02:43,130
So that means now this text is basically part of the class.

35
00:02:43,140 --> 00:02:48,690
It's not specific to a particular hotel, Right.

36
00:02:48,960 --> 00:02:51,130
That's why it is a class variable.

37
00:02:51,150 --> 00:02:57,450
Let me try to explain it better by creating some instances of the hotel class.

38
00:02:57,450 --> 00:03:02,100
So let's say hotel one is hotel ID is equal to.

39
00:03:02,370 --> 00:03:09,720
So this expects an ID actually its hotel ID, hotel ID is equal to.

40
00:03:09,870 --> 00:03:12,570
Well, let's give one of these IDs.

41
00:03:12,900 --> 00:03:17,400
Let's say 188 188.

42
00:03:18,980 --> 00:03:21,170
Let's create another a second instance.

43
00:03:21,170 --> 00:03:26,630
Hotel, two hotel, hotel ID that's equal to.

44
00:03:28,430 --> 00:03:29,810
134.

45
00:03:30,780 --> 00:03:39,640
Make sure to give them as strings because we are loading the ID field as a string here, the ID fields.

46
00:03:39,660 --> 00:03:42,390
These are read as strings inside Python.

47
00:03:42,870 --> 00:03:50,460
So 134 right now if I print out hotel one dot name.

48
00:03:52,770 --> 00:03:54,630
And I execute the script.

49
00:03:57,130 --> 00:03:59,890
I'll get Snow Palace, right.

50
00:03:59,920 --> 00:04:04,850
That's the value of the name instance variable.

51
00:04:04,870 --> 00:04:08,660
So this is a variable of this particular instance.

52
00:04:08,680 --> 00:04:12,970
And now if I do print hotel to that name.

53
00:04:15,420 --> 00:04:18,209
We'll get to sunny apartment.

54
00:04:18,720 --> 00:04:27,240
So because name gives us different values for different instances, that's why we call we call that

55
00:04:27,240 --> 00:04:29,430
variable an instance variable.

56
00:04:29,430 --> 00:04:30,100
Right.

57
00:04:30,120 --> 00:04:36,720
But now, if you do print hotel one dot watermark.

58
00:04:39,370 --> 00:04:40,720
And we do the same.

59
00:04:40,750 --> 00:04:44,050
Let me duplicate this with control D or command D.

60
00:04:44,320 --> 00:04:46,510
So let's do hotel to that watermark.

61
00:04:46,540 --> 00:04:51,910
You'll see that, of course, we'll get the same value for both of these print functions.

62
00:04:52,390 --> 00:05:01,120
So that makes Watermark a class variable because the value of the variable is shared among all instances.

63
00:05:01,120 --> 00:05:10,630
Plus you can also access that's variable using hotel dot watermark.

64
00:05:12,710 --> 00:05:18,700
Execute and you will see that we get three lines printed out.

65
00:05:18,710 --> 00:05:21,710
So hotel is the name of the class.

66
00:05:21,830 --> 00:05:25,160
So Watermark belongs to that class.

67
00:05:27,500 --> 00:05:33,290
If we do print hotel dot name, you're going to get an error.

68
00:05:35,880 --> 00:05:36,780
So here we go.

69
00:05:36,810 --> 00:05:43,860
Hotel has no attribute name because name is an attribute of the instances.

70
00:05:44,520 --> 00:05:51,120
Now, what's what's the benefit of having a class variable, and when should you have a class variable?

71
00:05:51,270 --> 00:06:00,420
Well, the benefit is that the value of that variable can be shared across all the instances as you

72
00:06:00,420 --> 00:06:01,140
saw.

73
00:06:01,320 --> 00:06:03,390
So that means.

74
00:06:04,400 --> 00:06:13,970
If you think that all the instances should have access to one same value as is the case here.

75
00:06:13,970 --> 00:06:19,130
So all these hotels are part of this real estate company.

76
00:06:19,130 --> 00:06:28,550
So the company perhaps they want to print out that watermark, that label somewhere in the tickets or

77
00:06:28,550 --> 00:06:29,450
anywhere.

78
00:06:29,750 --> 00:06:36,740
So if you have an HTML frontend, for example, you have a web app and you want to display that label

79
00:06:36,740 --> 00:06:46,040
on the web app, then you could get that label from the hotel class like we did here or from the hotel

80
00:06:46,040 --> 00:06:48,410
instance, depending on the scenario.

81
00:06:48,770 --> 00:06:54,320
So that's the difference between instance variables and class variables.

82
00:06:54,530 --> 00:06:56,360
So self is a variable.

83
00:06:56,360 --> 00:07:04,280
In the case of Hotel one, it holds the hotel one instance in the case of hotel two, self holds the

84
00:07:04,280 --> 00:07:05,570
hotel to instance.

85
00:07:06,340 --> 00:07:09,230
And then you get name out of self or watermark.

86
00:07:09,250 --> 00:07:15,130
So self has access to both the watermark and hotel ID and name and so on.

87
00:07:15,440 --> 00:07:16,780
Yeah, I hope that's clear.

88
00:07:16,780 --> 00:07:21,340
And in the next video we'll cover instance methods versus class methods.

89
00:07:21,370 --> 00:07:22,060
I'll see you there.

