1
00:00:00,610 --> 00:00:03,910
Hey, welcome to a new video on p.

2
00:00:05,120 --> 00:00:14,240
So far, we created two classes in our event Scraping app, an event class and an email class.

3
00:00:15,460 --> 00:00:20,680
Now we have two more functions which are floating around without a glass.

4
00:00:20,680 --> 00:00:30,690
So since we decided to make this an app, then it's a good idea to place all the functions inside classes.

5
00:00:30,700 --> 00:00:35,560
So what do you think should be the next class we should have here?

6
00:00:36,550 --> 00:00:41,060
So we have a store function and a read function.

7
00:00:41,080 --> 00:00:51,370
These two functions are working on a database, so this stores data in the database and this other function

8
00:00:51,370 --> 00:00:53,980
reads data from the database.

9
00:00:53,980 --> 00:01:02,260
So I think that these two should belong to a database class.

10
00:01:03,010 --> 00:01:07,150
So this is just the name I decided to give to this class.

11
00:01:07,150 --> 00:01:14,560
You can also call this database table or another name, so you should try to give your classes meaningful

12
00:01:14,560 --> 00:01:15,280
names.

13
00:01:15,400 --> 00:01:17,590
I'll just leave it database.

14
00:01:18,280 --> 00:01:20,830
Make some break lines in here.

15
00:01:22,320 --> 00:01:25,800
Add the self arguments to each of these.

16
00:01:26,190 --> 00:01:28,890
I'll cover today what self is.

17
00:01:29,070 --> 00:01:35,340
Now this class here is a bit more demanding that the other two classes.

18
00:01:37,100 --> 00:01:44,630
This class is also using this connection object in here.

19
00:01:45,050 --> 00:01:53,600
So first we establish a connection here and we store that connection in this variable, and then this

20
00:01:53,600 --> 00:01:57,560
class is using that variable or both.

21
00:01:57,590 --> 00:02:00,350
It's a better idea to have that.

22
00:02:02,510 --> 00:02:04,610
Expression inside the class.

23
00:02:04,610 --> 00:02:09,710
Since we are using classes now to organize our code, then what I'll do.

24
00:02:09,710 --> 00:02:12,320
I'll just cut this out from here.

25
00:02:14,210 --> 00:02:16,340
And where do we place that?

26
00:02:16,370 --> 00:02:24,500
So we have two methods the store methods and the read methods, and both methods use that connection

27
00:02:24,500 --> 00:02:25,610
variable.

28
00:02:25,730 --> 00:02:33,170
So one way is to have that expression in both methods here and here.

29
00:02:33,540 --> 00:02:34,220
Or both.

30
00:02:34,220 --> 00:02:37,730
This is not the recommended way to do this.

31
00:02:38,180 --> 00:02:44,900
We should place this expression somewhere where it is accessed by the two methods.

32
00:02:44,900 --> 00:02:52,520
So just once, instead of having repetitive codes like this, so I'll delete it from here and here and

33
00:02:52,520 --> 00:03:01,370
I'll add a special underscore underscore in it, underscore, underscore methods.

34
00:03:01,850 --> 00:03:09,020
So make sure that the method is named exactly like this underscore, underscore in it in lowercase letters,

35
00:03:09,020 --> 00:03:10,370
underscore, underscore.

36
00:03:10,370 --> 00:03:13,460
And this also has self argument as well.

37
00:03:13,460 --> 00:03:23,300
And the column and then the body of this method will be that expression connection is equal to you light

38
00:03:23,300 --> 00:03:26,190
that's connect, dot, dot, dot, db.

39
00:03:26,240 --> 00:03:32,960
Now this variable then should be accessed by the store and the reads functions.

40
00:03:32,990 --> 00:03:41,540
Currently, though, PA charm is giving us a clue that these methods do not have access yet to this

41
00:03:41,540 --> 00:03:42,440
connection variable.

42
00:03:42,440 --> 00:03:45,170
You see, it's underlined in red.

43
00:03:45,920 --> 00:03:49,730
It says unresolved reference connection.

44
00:03:49,730 --> 00:03:55,400
So basically this function, this method doesn't see that variable.

45
00:03:56,170 --> 00:04:00,520
To make that variable visible to these two methods.

46
00:04:00,580 --> 00:04:06,280
What we do is we add self dot in front.

47
00:04:06,960 --> 00:04:13,680
So now connection is a property of the self argument.

48
00:04:14,040 --> 00:04:22,350
Then in here we do the same self dot connection, self, dot connection.

49
00:04:22,410 --> 00:04:29,430
So now we point to that property to that variable, which is a property of self.

50
00:04:29,970 --> 00:04:37,620
And so self is like a special variable and all the methods have access to self, right?

51
00:04:37,860 --> 00:04:43,950
So we use self as the middleman to access the variables.

52
00:04:43,980 --> 00:04:46,710
So self dot connection, cell, dot connection.

53
00:04:46,710 --> 00:04:51,210
Same goes in here, self dot connection, and so on.

54
00:04:51,690 --> 00:05:01,440
And then when we call the the class here, down here we say if extracted and so on, we create a database

55
00:05:02,250 --> 00:05:09,090
instance using the database class, which is with a capital D, right.

56
00:05:09,570 --> 00:05:10,800
This class.

57
00:05:13,980 --> 00:05:23,910
And then in here we see a database with lowercase letters that reads and same here, database that store.

58
00:05:23,910 --> 00:05:28,370
And if you run the code now, it will work the same as it did before.

59
00:05:28,380 --> 00:05:37,080
Now, perhaps your data DB file has some data already, so make sure that you have an empty data DB

60
00:05:37,110 --> 00:05:37,590
file.

61
00:05:37,590 --> 00:05:43,620
Perhaps you need to redownload it from the lecture resources in the previous video so that you have

62
00:05:43,620 --> 00:05:49,080
a fresh empty database DB file and that will send you an email.

63
00:05:49,160 --> 00:05:54,060
If that's the case, when a tool is found, an event is found.

64
00:05:54,150 --> 00:06:01,110
But I guarantee you that this code works the same as it used to do when it had functions in here.

65
00:06:02,440 --> 00:06:11,890
Now, if you remember from the previous apps where we used to create PDF reports, we used to have.

66
00:06:14,300 --> 00:06:20,900
This line of code pdf is equal to f pdf where f pdf was a class.

67
00:06:20,960 --> 00:06:31,730
So a class which created PDF instances and this class got some arguments such as an orientation argument

68
00:06:31,730 --> 00:06:36,620
which was P or L or portrayed something like that.

69
00:06:36,860 --> 00:06:45,920
So this class used to get arguments of both our classes here database, event and email.

70
00:06:45,920 --> 00:06:53,390
They don't get any arguments, but we can also have them make it possible that they get arguments.

71
00:06:53,390 --> 00:07:00,200
So let's do that with this database class and then I'll explain you what self is and what in its method

72
00:07:00,200 --> 00:07:00,500
is.

73
00:07:00,500 --> 00:07:01,370
Exactly.

74
00:07:01,850 --> 00:07:08,870
So if you want this database class to get an arguments, you want to add that as a parameter of the

75
00:07:08,870 --> 00:07:12,020
init function or the init method.

76
00:07:12,680 --> 00:07:18,620
So the parameter could be, for example, database path.

77
00:07:18,890 --> 00:07:19,430
Right.

78
00:07:20,060 --> 00:07:23,120
And so this goes in here as well.

79
00:07:23,600 --> 00:07:25,880
Database path.

80
00:07:25,880 --> 00:07:26,570
Right.

81
00:07:27,440 --> 00:07:35,240
And then when you call the class here, let me fold this function.

82
00:07:36,250 --> 00:07:37,480
And this one in here.

83
00:07:37,600 --> 00:07:39,040
So I'm not deleting them.

84
00:07:39,040 --> 00:07:42,090
I'm just making them invisible for now.

85
00:07:42,100 --> 00:07:44,320
So the code is still there, right?

86
00:07:45,190 --> 00:07:46,030
I just want to do that.

87
00:07:46,030 --> 00:07:50,120
So you see the class and also these lines in here.

88
00:07:50,140 --> 00:07:59,920
So now in database, we say database path is equal to the value, which is the string data DB which

89
00:07:59,920 --> 00:08:02,620
points to this file.

90
00:08:03,250 --> 00:08:07,510
So now this class is getting an argument.

91
00:08:07,510 --> 00:08:10,180
So that's the argument and that's the value.

92
00:08:10,300 --> 00:08:19,510
So the way you give arguments to a class is by providing a parameter in the in its methods.

93
00:08:20,310 --> 00:08:23,270
So what is in it and what is self?

94
00:08:23,280 --> 00:08:24,150
Let me explain.

95
00:08:24,150 --> 00:08:31,110
These two very delicate concepts now in it is a special method, as I told you already.

96
00:08:31,110 --> 00:08:32,570
Why is it special?

97
00:08:32,580 --> 00:08:42,600
Well, it is special because this method is executed when you create an instance of the class.

98
00:08:43,289 --> 00:08:53,400
So specifically, when the interpreter runs this line here, this init method is called all the background.

99
00:08:53,400 --> 00:08:56,610
So perhaps if you had like a print function here.

100
00:08:57,690 --> 00:08:58,410
Hello.

101
00:08:58,410 --> 00:09:05,880
And you execute this code that hello will be printed out when the interpreter reaches this line here.

102
00:09:05,880 --> 00:09:06,570
Right.

103
00:09:06,570 --> 00:09:09,090
And you'll get hello in the command line.

104
00:09:09,870 --> 00:09:14,190
So I can also prove you that by opening the console here.

105
00:09:14,190 --> 00:09:16,050
Class food.

106
00:09:16,050 --> 00:09:17,550
Just a dummy class.

107
00:09:18,030 --> 00:09:27,240
Define an init method with self and not other arguments, but just print out hello like that.

108
00:09:27,330 --> 00:09:28,730
And then another method.

109
00:09:28,740 --> 00:09:31,740
Define do something with self.

110
00:09:33,150 --> 00:09:33,960
Print.

111
00:09:34,290 --> 00:09:37,200
I am the do method.

112
00:09:39,000 --> 00:09:41,250
Execute creates a new instance.

113
00:09:41,250 --> 00:09:42,210
Fu one.

114
00:09:42,240 --> 00:09:43,440
Equal to fu.

115
00:09:43,770 --> 00:09:51,150
When I execute that now hello will be printed out because this line here, as I mentioned, executes

116
00:09:51,150 --> 00:09:52,010
that method.

117
00:09:52,020 --> 00:10:01,080
But this other method is only executed when you point to foo one event to that method explicitly.

118
00:10:01,380 --> 00:10:03,270
So execute that and you'll get.

119
00:10:03,270 --> 00:10:04,830
I'm the do methods.

120
00:10:05,910 --> 00:10:07,440
Now what is self?

121
00:10:08,070 --> 00:10:12,120
Self is a special argument.

122
00:10:12,120 --> 00:10:15,270
So we have a special method and we have a special argument.

123
00:10:15,270 --> 00:10:15,930
Self.

124
00:10:16,290 --> 00:10:23,790
Self actually is the variable which holds the instance that is being created.

125
00:10:24,210 --> 00:10:25,710
So for example.

126
00:10:27,400 --> 00:10:29,140
In the food class here.

127
00:10:29,140 --> 00:10:31,240
This is an instance, right?

128
00:10:31,240 --> 00:10:33,610
And we can have multiple instances such as food.

129
00:10:33,610 --> 00:10:35,500
Two is equal to foo.

130
00:10:36,500 --> 00:10:37,190
Great.

131
00:10:38,150 --> 00:10:39,980
So we have a few one instance.

132
00:10:39,980 --> 00:10:40,700
We have a few.

133
00:10:40,760 --> 00:10:46,880
For instance, you'll see that these have different addresses in memory.

134
00:10:46,880 --> 00:10:49,130
So you see that this has this address.

135
00:10:49,160 --> 00:10:50,570
This has another address.

136
00:10:51,200 --> 00:10:56,510
These are just random IDs that are assigned by Python.

137
00:10:57,200 --> 00:11:02,870
So but I'm trying to tell you that these are like different objects, different instances.

138
00:11:03,640 --> 00:11:09,610
And so self is like the variable which holds the instance inside the class.

139
00:11:09,700 --> 00:11:16,890
And so when you say self, that connection, you are pointing to the connection of that particular instance.

140
00:11:16,900 --> 00:11:18,700
Let me try to explain it better.

141
00:11:18,700 --> 00:11:23,820
I know this is confusing, so let's create another class.

142
00:11:23,830 --> 00:11:30,640
I'll use an arrow key to call this previously executed statements.

143
00:11:30,640 --> 00:11:31,390
So the class.

144
00:11:31,390 --> 00:11:36,280
But this time I'll go up here and I'll add a new argument.

145
00:11:36,280 --> 00:11:38,470
Let's say X, right?

146
00:11:39,040 --> 00:11:42,790
And then I'll say self that x is equal to x.

147
00:11:44,370 --> 00:11:45,300
Execute.

148
00:11:45,660 --> 00:12:00,240
Now FU three is equal to FU with X being one and FU four is equal to FU again with x being two.

149
00:12:01,490 --> 00:12:09,470
Know fu three dot x gives you one while fu for that ex gives you two.

150
00:12:10,190 --> 00:12:17,060
So that proves my point that foo three and foo four are different instances.

151
00:12:17,570 --> 00:12:23,030
So you can try to imagine this like being the self argument.

152
00:12:23,750 --> 00:12:26,750
But outside the class it's not called self.

153
00:12:26,750 --> 00:12:29,510
It's called the variable name which holds that instance.

154
00:12:29,510 --> 00:12:32,720
But inside the class it's self.

155
00:12:33,140 --> 00:12:38,240
So basically foo three dot x is self, dot x.

156
00:12:38,420 --> 00:12:46,070
So when the user gave the x value to x here, that value went in here first.

157
00:12:46,070 --> 00:12:49,100
So x is equal to one, right?

158
00:12:49,250 --> 00:12:53,660
And then self does x is equal to one.

159
00:12:53,660 --> 00:12:57,420
So this x here is that x, right.

160
00:12:57,440 --> 00:13:01,160
So one is assigned to self that x.

161
00:13:01,160 --> 00:13:05,900
So that's the property of self and that's just an argument.

162
00:13:05,930 --> 00:13:13,010
It's a bit confusing, but that's why we have applications and you'll see how you'll use self in different

163
00:13:13,010 --> 00:13:16,070
scenarios and that will become second nature to you.

164
00:13:16,760 --> 00:13:17,870
But through now.

165
00:13:18,870 --> 00:13:20,220
This is what you should remember.

166
00:13:20,220 --> 00:13:23,940
So let's make a revision of what we did in this video.

167
00:13:24,390 --> 00:13:32,040
So you learn the init method which is executed when you instantiate the class, when you create an instance

168
00:13:32,040 --> 00:13:32,790
of the class.

169
00:13:32,790 --> 00:13:40,470
So init is executed in here, then we have self, which is a variable inside that class and it holds

170
00:13:40,470 --> 00:13:43,650
the instance that the user is creating.

171
00:13:43,650 --> 00:13:47,730
And then we have other methods such as tool and reads.

172
00:13:47,920 --> 00:13:56,670
And then we have properties such as connection here or this X here, right?

173
00:13:57,210 --> 00:14:05,280
That's a property, that's a method and that's what you should remember for now.

174
00:14:05,280 --> 00:14:09,930
So these are concepts that we will revise again using different examples.

175
00:14:09,930 --> 00:14:14,730
So don't worry if you don't get things right away and I'll talk to you in the next videos.

