1
00:00:00,330 --> 00:00:06,510
Well, in the previous lectures, I mentioned that I'll be creating a program, an application where

2
00:00:06,510 --> 00:00:13,500
we'll be handling sort of bank account so that we will have a balance and then we will be applying

3
00:00:13,500 --> 00:00:20,250
some operations, such as withdraw money from this bank account object and deposit money as

4
00:00:20,250 --> 00:00:20,500
well.

5
00:00:20,950 --> 00:00:25,880
I'll be storing the value, so the balance in a simple text file.

6
00:00:27,030 --> 00:00:30,540
So that's not one of the 10 programs we're building in the course.

7
00:00:30,900 --> 00:00:38,380
This is just to get you familiar with classes and to explain them a little bit more so.

8
00:00:40,080 --> 00:00:41,850
I'll do some housekeeping here.

9
00:00:42,090 --> 00:00:51,600
So let me create a folder here and put all these existing files.

10
00:00:53,720 --> 00:01:01,020
In this folder, so we have the previous files in here and I'll create a new folder now.

11
00:01:01,040 --> 00:01:06,190
Let's call this account and in this folder, I'll have my Python script.

12
00:01:07,340 --> 00:01:09,080
So let's call it like this.

13
00:01:10,730 --> 00:01:16,960
So under account, we have this Python file and we also need this text file.

14
00:01:18,290 --> 00:01:22,590
We will be saving the current balance of our bank account.

15
00:01:23,420 --> 00:01:30,410
So this will be a trivial application because in real life, you don't want to save the bank account information,

16
00:01:30,410 --> 00:01:36,500
the balance in a text file, in real life you'd want to save that in a real database

17
00:01:36,770 --> 00:01:38,810
such as PostgreSQL for instance.

18
00:01:39,620 --> 00:01:46,550
But for the sake of practicing object oriented programming, we will just say the balance value in this

19
00:01:46,760 --> 00:01:47,160
file.

20
00:01:47,780 --> 00:01:56,400
So this is our storage and also for the sake of simplicity, we will be working only on one single account.

21
00:01:57,110 --> 00:02:04,400
So our program will be able to process one account object because if you have multiple accounts, then

22
00:02:05,180 --> 00:02:07,720
the solution could be different from this.

23
00:02:08,480 --> 00:02:14,000
So you'd probably have to think about having a table and maybe a database would be necessary here.

24
00:02:15,200 --> 00:02:19,050
So let's think about our personal accounts.

25
00:02:19,490 --> 00:02:25,760
You could use this program for your personal notes, so you may want to keep track of your balance and

26
00:02:25,760 --> 00:02:29,750
you can use this Python program. Now the object

27
00:02:29,750 --> 00:02:34,340
here is the bank account, and now about this object,

28
00:02:34,400 --> 00:02:43,600
we should start thinking about what attributes this bank account object should have as an initial state.

29
00:02:44,390 --> 00:02:51,440
So we talked about this minimal object and a bank account would have, of course, an initial balance.

30
00:02:51,830 --> 00:02:56,330
So let's start to create this account object and call it account.

31
00:02:56,930 --> 00:02:59,710
You can call it whatever you want, but I'll call it account.

32
00:02:59,710 --> 00:03:07,760
So in it, here's the first function we want to define and then we pass self in there.

33
00:03:08,180 --> 00:03:11,360
And let's keep it like that for a moment.

34
00:03:11,750 --> 00:03:16,040
And we said our bank account object will have a balance attribute.

35
00:03:16,370 --> 00:03:25,430
And in our case, we have this balance.txt. Let's create an initial value there.

36
00:03:26,090 --> 00:03:35,930
And now, to create an object instance, we need to grab this value and pass it to our init function

37
00:03:36,410 --> 00:03:39,740
so that we construct this minimal object.

38
00:03:40,700 --> 00:03:47,570
And because we have a file, what we need to do is we need to read this number from this file.

39
00:03:48,200 --> 00:03:50,520
And really what you want to pass

40
00:03:50,540 --> 00:03:54,820
here is some parameter like filepath.

41
00:03:55,250 --> 00:04:03,890
So this is how I'm going to call that and now if you remember in the Bookstore Application, in the

42
00:04:03,890 --> 00:04:11,240
backend script here, we also pass a parameter to the Init function and then we created a connection

43
00:04:11,240 --> 00:04:14,660
object out of this database file.

44
00:04:15,500 --> 00:04:22,160
In this particular case, what we want to create is an integer.

45
00:04:22,460 --> 00:04:27,560
So a number out of this file, out of this path.

46
00:04:28,100 --> 00:04:32,480
So you could do something like with open.

47
00:04:32,690 --> 00:04:40,240
So we're doing file handling with the with method, with open filepath.

48
00:04:41,360 --> 00:04:43,070
And this is a text file.

49
00:04:43,070 --> 00:04:47,860
So we need to go with 'r' mode, which means read.

50
00:04:48,290 --> 00:04:51,590
So we're opening the file path,

51
00:04:52,220 --> 00:04:57,120
this file in read mode as a file.

52
00:04:57,320 --> 00:05:04,730
So this is just a temporary variable that exists in this with statement.

53
00:05:05,180 --> 00:05:07,530
And then we need to read from this file.

54
00:05:07,970 --> 00:05:10,180
So something like file.read.

55
00:05:10,580 --> 00:05:11,000
And yeah.

56
00:05:11,030 --> 00:05:11,510
That's it.

57
00:05:11,690 --> 00:05:14,910
This will read the file, but you're not storing that anywhere.

58
00:05:14,930 --> 00:05:23,270
So what you want to do is say: self.balance=file.read().

59
00:05:23,990 --> 00:05:29,010
So what that will do, it will save the value inside the text file.

60
00:05:29,030 --> 00:05:30,420
In this instance, variable.

61
00:05:30,440 --> 00:05:33,040
So this is an instance variable, that is how it is called.

62
00:05:33,050 --> 00:05:38,300
Actually this is the instance variable and this is the object.

63
00:05:38,750 --> 00:05:41,530
So the account object, good.

64
00:05:42,020 --> 00:05:43,720
This is almost good.

65
00:05:44,520 --> 00:05:52,220
And we also need to add the integer function there because this will be read as a string by default.

66
00:05:52,380 --> 00:05:52,760
So we need

67
00:05:52,830 --> 00:06:00,120
to convert that value to an integer and yeah, that should work and that should construct the object

68
00:06:00,120 --> 00:06:05,150
for us, the account object with a balance attached to it.

69
00:06:05,580 --> 00:06:14,310
And before going and creating the withdrawal method here and the deposit method,

70
00:06:14,310 --> 00:06:20,900
I'd prefer to call an instance of this class first. So I would do that here.

71
00:06:20,940 --> 00:06:24,990
And let's say saw that in the account variable.

72
00:06:25,170 --> 00:06:32,790
So this will be the object, the object that will be created out of these blueprints, the account blueprints.

73
00:06:33,270 --> 00:06:43,490
And there you need to pass this argument, the argument for this parameter, which is balance.txt.

74
00:06:44,610 --> 00:06:50,150
Great and for self python will automatically pass the account object instance.

75
00:06:51,150 --> 00:06:51,820
Good.

76
00:06:52,590 --> 00:06:54,990
Now, this will create an object instance.

77
00:06:55,350 --> 00:07:07,250
And let me print out that object here so we see some output in action python and that's inside the account

78
00:07:07,260 --> 00:07:07,940
folder.

79
00:07:08,340 --> 00:07:16,030
And acc.py. We got an error: No such file or directory for balance.txt.

80
00:07:16,410 --> 00:07:21,720
And the reason for that is that we need to pass the account directory there as well.

81
00:07:22,110 --> 00:07:22,440
So.

82
00:07:24,830 --> 00:07:30,710
And yeah, this time it's working, so we had to pass the directory there because we are in this folder, in

83
00:07:30,710 --> 00:07:36,210
the demo folder, and you need to pass the account folder first and then point 

84
00:07:36,210 --> 00:07:36,660
to balance.txt.

85
00:07:37,370 --> 00:07:39,910
And this is the output.

86
00:07:41,870 --> 00:07:44,510
So it says __main__.Account object.

87
00:07:44,810 --> 00:07:55,130
So the account object of the main module and what main is, is when you execute a script like we did

88
00:07:55,130 --> 00:07:55,460
here.

89
00:07:56,390 --> 00:07:58,800
So you point to the script with Python.

90
00:07:59,780 --> 00:08:03,990
Python will assign this name to the module.

91
00:08:04,190 --> 00:08:09,080
So this module will get this name and the script, actually.

92
00:08:09,080 --> 00:08:11,570
But it's it's treated as a module.

93
00:08:12,530 --> 00:08:16,090
But if you import that module.

94
00:08:16,100 --> 00:08:19,690
So if I run, I open an interactive shell now.

95
00:08:20,330 --> 00:08:22,760
So from the account folder import acc.

96
00:08:25,640 --> 00:08:33,890
What happens is that you get the print function executed as well, but this time when you import

97
00:08:34,460 --> 00:08:35,340
a file.

98
00:08:35,540 --> 00:08:43,640
So when you don't executed like we did previously with Python account.acc.py, when you import the

99
00:08:43,790 --> 00:08:48,380
module, the module gets the name of the script that it is written to.

100
00:08:48,800 --> 00:08:58,070
So in this case, this acc and without the extension.
Here, you also get the name of the package of

101
00:08:58,070 --> 00:08:58,760
the module.

102
00:08:59,060 --> 00:09:03,770
So packages are made of modules and a package is like a folder.

103
00:09:03,950 --> 00:09:10,790
There you have several module modules or one module and like we have in this case and our package here

104
00:09:10,790 --> 00:09:13,490
is account.

105
00:09:13,490 --> 00:09:18,570
Then it has this module and then this module has this class, the account class.

106
00:09:20,990 --> 00:09:23,000
So that was something I wanted to show you.

107
00:09:23,840 --> 00:09:26,150
Packages, modules and classes.

108
00:09:27,440 --> 00:09:27,740
Great.

109
00:09:27,740 --> 00:09:34,460
Now I'll exit this Python Shell and play around with this script a little bit.

110
00:09:35,250 --> 00:09:42,740
And what we did here, we printed all the object namespace and we didn't get the actual balance

111
00:09:42,800 --> 00:09:46,390
as an output because we are not returning the balance here.

112
00:09:46,760 --> 00:09:53,180
But if you want to get the balance, you'd want to point to the balance.

113
00:09:54,350 --> 00:09:59,960
And that uses the dot notation to point to the object that holds the balance attribute.

114
00:10:00,600 --> 00:10:06,350
So let's see what we get this time. Yeah 1000.

115
00:10:06,750 --> 00:10:08,330
So that's the current balance.

116
00:10:08,690 --> 00:10:15,740
As you can see, you are able to access attributes of your object instance using the dot notation.

117
00:10:16,100 --> 00:10:16,420
Good.

118
00:10:16,700 --> 00:10:19,280
Now, let's add these other methods there.

119
00:10:20,600 --> 00:10:23,000
And what can we do to a bank account?

120
00:10:23,030 --> 00:10:31,640
Well, we can withdraw money and you always need to pass self in all of your methods.

121
00:10:31,940 --> 00:10:38,840
And then the parameter that applies to that particular method in this case, when you withdraw some

122
00:10:38,840 --> 00:10:43,200
money, well, you have an amount in mind that you want to withdraw.

123
00:10:43,580 --> 00:10:47,630
So you pass amount there as a parameter.

124
00:10:48,170 --> 00:10:56,060
And just to be clear that you understand what I'm doing, what I'm doing here is I have an initial balance

125
00:10:56,600 --> 00:10:59,910
that is being read from this file.

126
00:11:00,590 --> 00:11:08,630
So for this text file and then I want to allow the user to apply operations or withdraw money from this

127
00:11:08,630 --> 00:11:09,020
balance.

128
00:11:09,200 --> 00:11:17,570
And once the user executes the withdrawal method, I want to update the balance number inside the object

129
00:11:17,670 --> 00:11:18,050
instance.

130
00:11:19,070 --> 00:11:24,340
And I don't want yet to write that balance to two to my balance file.

131
00:11:24,620 --> 00:11:33,800
So I just want to calculate something here like self.balance=self.balance.

132
00:11:34,430 --> 00:11:37,860
Well, this is a withdrawal, so minus the amount that I want to withdraw.

133
00:11:38,210 --> 00:11:40,090
That means this will do

134
00:11:40,100 --> 00:11:46,610
self.balance equals to 1000 minus 100 let's say, and I'll end up with an updated balance of

135
00:11:47,180 --> 00:11:47,870
900

136
00:11:48,500 --> 00:11:49,280
in this case.

137
00:11:50,170 --> 00:11:59,710
And that if I want, I can go ahead and open that file again here and then write this updated balance

138
00:11:59,710 --> 00:12:02,500
in that file, so that would be one solution.

139
00:12:03,340 --> 00:12:10,750
But a more constructive solution would probably be to have specific methods that commits the changes

140
00:12:10,960 --> 00:12:12,430
to the file.

141
00:12:12,800 --> 00:12:18,970
So you may want to withdraw some money and then you deposit some money in the same session and then

142
00:12:18,970 --> 00:12:21,550
you apply the committee method to save the changes.

143
00:12:21,940 --> 00:12:23,070
So this is the solution

144
00:12:23,080 --> 00:12:27,490
I want to go for. We are done with the withdrawal method.

145
00:12:27,550 --> 00:12:35,480
Now deposit(self, amount) again, so this is a local variable.

146
00:12:35,500 --> 00:12:36,600
This is a local variable.

147
00:12:37,510 --> 00:12:42,580
They happen to have the same name, but they don't have to do much with each other.

148
00:12:44,410 --> 00:12:55,040
And self.balance equals to self.balance plus the amount in this case.

149
00:12:55,360 --> 00:12:57,940
So let's see what we have this far.

150
00:12:58,540 --> 00:13:04,060
And I'll keep this instance object here that is being generated.

151
00:13:04,780 --> 00:13:12,240
So we are instantiating an object instance, in other words, and print out the current balance and

152
00:13:12,250 --> 00:13:16,930
then I want to withdraw some money from my account object.

153
00:13:18,110 --> 00:13:25,720
That means I apply, withdraw to the account object and withdraw takes two arguments.

154
00:13:26,230 --> 00:13:32,830
So the object instance which is passed automatically and then the amounts which could be one hundred.

155
00:13:34,360 --> 00:13:40,540
And once I do that, I want to print out the updated balance.

156
00:13:41,620 --> 00:13:43,640
So let's see what it's going to do.

157
00:13:45,280 --> 00:13:45,610
Yeah.

158
00:13:45,820 --> 00:13:46,440
1000.

159
00:13:46,810 --> 00:13:56,950
So an object is created and you get 1000 printed out because here you have 1000 and then you take 100

160
00:13:56,950 --> 00:13:57,520
out of that,

161
00:13:57,520 --> 00:14:01,270
and so you end up with 900, great.

162
00:14:01,330 --> 00:14:09,340
So if you run that again, you will notice that a new object instance is being created again.

163
00:14:10,090 --> 00:14:16,000
So you don't have 900 as the current balance anymore, you have 1000.

164
00:14:16,660 --> 00:14:21,460
So that means you need to write the changes to the file

165
00:14:22,340 --> 00:14:31,970
if you wanted to save that balance, and we can do that using a method and let's call it commit.

166
00:14:33,710 --> 00:14:41,780
And pass self there and I don't think of any parameters that I need to pass through this commit method

167
00:14:42,530 --> 00:14:47,380
because all we will work with are the existing variables there.

168
00:14:48,680 --> 00:14:51,700
So this would be something like: with open.

169
00:14:52,970 --> 00:15:00,320
Well, we need to open the file path, but we have a small problem in here because the file path

170
00:15:00,330 --> 00:15:05,580
Variable now is just a local variable in this init method.

171
00:15:06,380 --> 00:15:11,820
So if we pass it here, this function, this method, we will not recognize it.

172
00:15:13,520 --> 00:15:20,870
So this is a bit different from the other the backend script that we wrote, because here we have this

173
00:15:21,530 --> 00:15:23,840
parameter like we do in here.

174
00:15:24,470 --> 00:15:31,040
But in this case, this parameter, it was only used inside the init method, so we didn't use

175
00:15:31,040 --> 00:15:32,180
it anywhere else.

176
00:15:33,800 --> 00:15:36,170
So there is a solution to that.

177
00:15:37,410 --> 00:15:41,540
And one solution would be to go ahead and pass another.

178
00:15:41,930 --> 00:15:43,370
Let's call this path here.

179
00:15:43,730 --> 00:15:46,250
So another parameter to the committee methods.

180
00:15:47,120 --> 00:15:54,230
Then when we call the committee method like we were doing with withdrawal, we'd have to pass the balance.txt.

181
00:15:55,100 --> 00:16:03,350
Actually this path to the commit method while that would work, it's still not the best practice

182
00:16:03,350 --> 00:16:06,860
of object oriented paradigm.

183
00:16:07,280 --> 00:16:10,280
So we need to seek for another solution.

184
00:16:10,910 --> 00:16:15,530
And that would be to make this local variable as an instance variable.

185
00:16:16,310 --> 00:16:22,760
And to do that you just do something like self.filepath=filepath.

186
00:16:25,580 --> 00:16:32,510
And what this does is... So file path, don't confuse this with this, this happened to be the same, but

187
00:16:32,510 --> 00:16:37,100
you can also write another thing here, like anything.

188
00:16:38,210 --> 00:16:41,770
So this is the parameter of the init function.

189
00:16:41,990 --> 00:16:47,020
So the balance.txt will go to this parameter.

190
00:16:47,360 --> 00:16:54,050
So the path of this file will go to this parameter and then you create this instance variable and that

191
00:16:54,050 --> 00:16:57,110
instance variable will be equal to this path.

192
00:16:57,600 --> 00:17:03,890
But you can use this thing here, you can use it wherever you like inside your class.

193
00:17:04,430 --> 00:17:07,850
And in our case, we would like to use it in here.

194
00:17:11,270 --> 00:17:18,530
So we open themselves up to the fall path in the write method this time because at this time we need

195
00:17:18,530 --> 00:17:26,170
to write or more accurately to overwrite the current balance, which should be this one.

196
00:17:26,180 --> 00:17:29,570
For example, we want to overwrite that to this file.

197
00:17:32,160 --> 00:17:38,630
So as file, this is just a temporary variable. File and file.write.

198
00:17:39,470 --> 00:17:42,560
And self.balance.

199
00:17:50,820 --> 00:17:51,160
Good.

200
00:17:51,540 --> 00:17:52,650
Let's try this out.

201
00:17:59,670 --> 00:18:09,090
And we didn't get any of the in the file here because I forgot to actually call the commit method and so once

202
00:18:09,120 --> 00:18:12,630
we withdraw money, let's bring that out in the console.

203
00:18:13,020 --> 00:18:23,400
And then account.commit without passing any arguments explicitly, because implicitly we are passing

204
00:18:23,400 --> 00:18:32,130
the object instance. Good. We got an error there invalid literal for integer with base 10.

205
00:18:32,940 --> 00:18:41,390
So this would be the integer in line six here and so on.

206
00:18:42,330 --> 00:18:47,050
The value that is being read by this method is not converting to an integer.

207
00:18:48,270 --> 00:18:55,230
And the reason is that this value has disappeared from balance.txt, and this is reading

208
00:18:55,230 --> 00:18:55,970
an empty string.

209
00:18:55,980 --> 00:19:00,540
So this is an empty string that is being read from this file.

210
00:19:01,110 --> 00:19:03,630
And then you try to convert that and it doesn't work.

211
00:19:04,350 --> 00:19:12,530
Now, the reason that we got this empty string and empty file here is when we got the previous error,

212
00:19:14,040 --> 00:19:17,880
so here a right argument must be string, not integer.

213
00:19:18,660 --> 00:19:27,240
So what Python did was it opened the file with the method, but then it wasn't able to actually write

214
00:19:27,240 --> 00:19:28,770
a value inside that file.

215
00:19:28,900 --> 00:19:31,940
So the file ended up empty.

216
00:19:32,780 --> 00:19:33,080
Hmm.

217
00:19:33,300 --> 00:19:43,530
So let me write the initial value there and then now we should be able to run that again.

218
00:19:47,530 --> 00:19:50,810
And yeah, now we have the updated value.

219
00:19:52,330 --> 00:19:57,820
So, again, what we did is we created the object instance and then we put it in the current balance,

220
00:19:57,880 --> 00:20:05,200
which was read by from this fall, which was one thousandth of the beginning, and then we withdrew

221
00:20:05,380 --> 00:20:05,950
100.

222
00:20:05,980 --> 00:20:12,250
So the balance was updated and then we committed those changes to this file.

223
00:20:12,260 --> 00:20:14,690
So we get 900 there now.

224
00:20:14,740 --> 00:20:23,650
Same thing if you want to add, let's say 200 and deposit.

225
00:20:26,620 --> 00:20:28,000
Let's see if this will work.

226
00:20:30,400 --> 00:20:32,130
Yeah, this was printed out.

227
00:20:32,220 --> 00:20:33,130
Let's check the file.

228
00:20:33,630 --> 00:20:43,260
Yeah, so this is our new balance and I hope this clarifies a few concepts about object oriented programming.

229
00:20:43,980 --> 00:20:50,820
If you are creative, you can go ahead and expand the script so you can add more methods there and maybe

230
00:20:50,820 --> 00:20:54,150
make this more of a real life application.

231
00:20:55,020 --> 00:20:55,540
So great.

232
00:20:55,560 --> 00:20:58,050
And this is about this lecture.

233
00:20:58,050 --> 00:21:04,740
And in the next lecture, I'll talk about inheritance and we will be expanding this script by applying

234
00:21:04,740 --> 00:21:06,420
inheritance in there.

235
00:21:06,780 --> 00:21:09,260
So talk to you in the next lecture.

