1 00:00:00,000 --> 00:00:02,128 Well, in the previous lectures, I 2 00:00:02,129 --> 00:00:04,229 mentioned that I'll be creating a 3 00:00:04,439 --> 00:00:07,109 program and application where we'll be 4 00:00:07,169 --> 00:00:11,159 handling, sort of bank account. So that 5 00:00:11,159 --> 00:00:12,959 will have a balance. And then we will be 6 00:00:12,989 --> 00:00:15,329 applying some operations such as 7 00:00:15,359 --> 00:00:18,329 withdraw money from his bank account 8 00:00:18,359 --> 00:00:21,269 object, and deposit money as well. I will 9 00:00:21,269 --> 00:00:24,659 be storing the value, so the balance in a 10 00:00:24,659 --> 00:00:28,529 simple text file. So that's not one of 11 00:00:28,559 --> 00:00:30,119 10 programs, so we're be building in the 12 00:00:30,119 --> 00:00:32,369 course. And this is just to get you 13 00:00:32,369 --> 00:00:35,159 familiar with classes, and to explain 14 00:00:35,159 --> 00:00:40,709 them a little bit more. So I'll do some 15 00:00:40,709 --> 00:00:44,519 housekeeping here. So let me create a folder here. 16 00:00:44,520 --> 00:00:46,849 [Author typing] 17 00:00:46,851 --> 00:00:51,582 And put all these existing files 18 00:00:51,584 --> 00:00:53,597 [No audio] 19 00:00:53,598 --> 00:00:56,729 in this folder, so we have the 20 00:00:56,729 --> 00:01:00,029 previous files in here, and I'll create 21 00:01:00,059 --> 00:01:02,249 a new folder now, let's call this 22 00:01:02,279 --> 00:01:04,769 account. And in this folder, I'll have 23 00:01:04,799 --> 00:01:08,729 my Python script. So let's call it like 24 00:01:08,729 --> 00:01:12,359 this. So on the account, we have this 25 00:01:12,359 --> 00:01:16,499 Python file, and we also need these text 26 00:01:16,499 --> 00:01:20,069 file where we'll be saving the current 27 00:01:20,069 --> 00:01:23,819 balance of our bank account. So this 28 00:01:23,819 --> 00:01:26,129 will be a trivial application, because 29 00:01:26,129 --> 00:01:29,549 you realize you don't want to save the 30 00:01:29,669 --> 00:01:31,199 bank account information and the 31 00:01:31,199 --> 00:01:33,899 balance in a text file. Here you like, you'd 32 00:01:33,899 --> 00:01:37,079 want to save that in real database, such 33 00:01:37,079 --> 00:01:40,109 as PostgreSQL, for instance. But for the 34 00:01:40,109 --> 00:01:42,179 sake of practicing Object Oriented 35 00:01:42,179 --> 00:01:44,729 Programming, we will just save the 36 00:01:44,849 --> 00:01:48,449 balance value in this file. So this is 37 00:01:48,449 --> 00:01:51,569 our storage. And also for the sake of 38 00:01:51,569 --> 00:01:55,079 simplicity, we'll be working only on one 39 00:01:55,229 --> 00:01:58,409 single account. So our program will be 40 00:01:58,409 --> 00:02:00,809 able to process one account object. 41 00:02:01,649 --> 00:02:04,049 Because if you have multiple accounts, 42 00:02:04,049 --> 00:02:07,139 then the solution could be different 43 00:02:07,139 --> 00:02:10,379 from lists. So you'd probably have to 44 00:02:10,379 --> 00:02:12,389 think about having a table and maybe a 45 00:02:12,389 --> 00:02:15,569 database would be necessary here. So 46 00:02:15,569 --> 00:02:19,139 let's think about our personal account, 47 00:02:19,439 --> 00:02:21,959 you could use this program for your 48 00:02:21,959 --> 00:02:24,449 personal notes. So you may want to keep 49 00:02:24,449 --> 00:02:26,249 track of your balance, and you can use 50 00:02:26,249 --> 00:02:29,699 this Python program. And now the object 51 00:02:29,699 --> 00:02:33,689 here is the bank account. And of this 52 00:02:33,719 --> 00:02:36,119 object, we should start thinking about 53 00:02:36,149 --> 00:02:40,379 what attributes this bank account object 54 00:02:40,499 --> 00:02:44,669 should have as an initial state. So we 55 00:02:44,669 --> 00:02:47,969 talked about this minimal object. And a 56 00:02:47,969 --> 00:02:50,309 bank account would have, of course, an 57 00:02:50,309 --> 00:02:53,189 initial balance. So let's start to 58 00:02:53,189 --> 00:02:55,679 create this account object and call it 59 00:02:55,709 --> 00:02:57,959 Account. You can call it whatever you 60 00:02:57,959 --> 00:03:00,007 want, but I'll call it Account so, 61 00:03:01,187 --> 00:03:03,479 init, here's the first function we want to 62 00:03:03,479 --> 00:03:07,769 define, I would pass self in there. 63 00:03:08,189 --> 00:03:10,679 Anyhow let's keep it like that for a 64 00:03:10,679 --> 00:03:13,859 moment. And we said, our bank account 65 00:03:13,889 --> 00:03:16,019 object will have a balance attribute. 66 00:03:16,409 --> 00:03:22,093 And in our case, we have this balance.txt. 67 00:03:22,374 --> 00:03:26,726 And let's create an initial value there. And now, 68 00:03:27,839 --> 00:03:32,639 to create an object instance, we need to grab this 69 00:03:32,639 --> 00:03:35,879 value and pass it to our init function, 70 00:03:36,329 --> 00:03:39,059 so that we can start this minimal 71 00:03:39,089 --> 00:03:43,049 object. And because we have a file, what 72 00:03:43,049 --> 00:03:46,379 we need to do is we need to read this 73 00:03:46,379 --> 00:03:49,259 number from this file. And naturally, 74 00:03:49,289 --> 00:03:52,259 what you'd want to pass here is some 75 00:03:52,409 --> 00:03:55,949 parameter like file path. So this is how 76 00:03:55,949 --> 00:04:00,059 I'm going to call that. And now if you 77 00:04:00,059 --> 00:04:02,939 remember in the bookstore application, 78 00:04:03,569 --> 00:04:06,179 in the backend script, here, we also 79 00:04:06,179 --> 00:04:08,249 pass a parameter to the init function. 80 00:04:09,509 --> 00:04:11,729 And then we created a connection object 81 00:04:11,759 --> 00:04:16,379 out of this database file. In this 82 00:04:16,949 --> 00:04:20,309 particular case, what we want to create 83 00:04:20,579 --> 00:04:25,919 is an integer. So a number out of this file, 84 00:04:25,949 --> 00:04:29,009 out of this filepath. So you could do 85 00:04:29,009 --> 00:04:33,419 something like with open so we're doing 86 00:04:33,539 --> 00:04:37,049 file handling with, the with methods 87 00:04:37,709 --> 00:04:42,629 with open filepath. And this is a text 88 00:04:42,629 --> 00:04:46,829 file, so we need to go with our mode, 89 00:04:46,859 --> 00:04:49,259 which means to read. So we're opening 90 00:04:49,589 --> 00:04:53,549 the file pass through the file, this file, a 91 00:04:53,549 --> 00:04:58,709 read mode as an as a file. So this is just 92 00:04:58,739 --> 00:05:02,609 a temporary variable that exists in this 93 00:05:03,119 --> 00:05:06,119 with a statement. And then we need to 94 00:05:06,119 --> 00:05:08,909 read from this file. So something like 95 00:05:08,909 --> 00:05:12,179 file.read, and yeah, that's it. This will 96 00:05:12,179 --> 00:05:13,559 read the file, but you're not 97 00:05:13,559 --> 00:05:16,139 storing that anywhere. So what you want 98 00:05:16,139 --> 00:05:21,689 to do is, say self.balance equals to 99 00:05:21,689 --> 00:05:25,529 file.read. So what that will do, it 100 00:05:25,529 --> 00:05:28,949 will save the value inside the text file 101 00:05:28,979 --> 00:05:31,169 in this instance variable. So this is an 102 00:05:31,169 --> 00:05:33,719 instance variable that is how it's called. 103 00:05:34,349 --> 00:05:36,779 Actually, this is the instance variable, 104 00:05:36,809 --> 00:05:39,479 and this is the object. So the Account 105 00:05:39,509 --> 00:05:45,209 object, good, this is almost good. And we 106 00:05:45,209 --> 00:05:48,809 also need to add the integer function 107 00:05:48,809 --> 00:05:51,119 there, because this will be read as a 108 00:05:51,119 --> 00:05:53,489 string by default. So we need to convert 109 00:05:53,489 --> 00:05:56,849 that value to an integer. And yeah, that 110 00:05:56,849 --> 00:05:59,009 should work and that should construct 111 00:05:59,009 --> 00:06:01,679 the object for also the Account object 112 00:06:02,489 --> 00:06:05,909 with a balance attached to it. And 113 00:06:05,909 --> 00:06:09,839 before going and creating the, like a 114 00:06:09,839 --> 00:06:11,699 withdrawal methods here, and then 115 00:06:11,729 --> 00:06:16,199 deposit methods, I'll prefer to call an 116 00:06:16,199 --> 00:06:20,009 instance of this class first. So how to 117 00:06:20,009 --> 00:06:23,459 do that here? Let's say store that in the 118 00:06:23,459 --> 00:06:26,459 account variable, so this will be the 119 00:06:26,489 --> 00:06:29,429 object, the object that will be created 120 00:06:29,429 --> 00:06:32,069 out of these blueprints, the Account 121 00:06:32,077 --> 00:06:36,817 blueprint. And there you need to pass this 122 00:06:36,869 --> 00:06:39,539 argument, the argument for this 123 00:06:39,539 --> 00:06:43,499 parameter, which is balance.txt. 124 00:06:44,609 --> 00:06:47,219 Great. And for self, Python will 125 00:06:47,249 --> 00:06:50,519 automatically pass the Account object instance. 126 00:06:51,119 --> 00:06:54,449 Good. Now, this will create an object 127 00:06:54,449 --> 00:06:59,489 instance. And let me print out that 128 00:06:59,489 --> 00:07:03,089 object here. So we see some output in 129 00:07:03,089 --> 00:07:06,779 action. Python, that's inside the 130 00:07:06,779 --> 00:07:12,479 account folder, and acc.py. We got an 131 00:07:12,479 --> 00:07:14,549 error, no such file or directory for 132 00:07:14,549 --> 00:07:17,249 balance.txt. And the reason to that, 133 00:07:17,249 --> 00:07:19,439 is that we need to pass the account 134 00:07:20,099 --> 00:07:22,444 directory there as well. So 135 00:07:22,445 --> 00:07:24,845 [No audio] 136 00:07:24,847 --> 00:07:28,169 anyhow this time it's working. So we had to pass the 137 00:07:28,169 --> 00:07:30,089 directory there, because we are in this 138 00:07:30,089 --> 00:07:32,249 folder in the Demo folder. And you need 139 00:07:32,249 --> 00:07:34,319 to pass the account folder first, and 140 00:07:34,319 --> 00:07:37,739 then point to the balance.txt. And 141 00:07:37,739 --> 00:07:44,437 this is the output. So it says main.Account object. 142 00:07:44,777 --> 00:07:49,211 So the Account object of the main module, 143 00:07:49,688 --> 00:07:54,149 and what main is, is when you execute a script, 144 00:07:54,297 --> 00:07:58,109 like we did here. So you pointed to the script with 145 00:07:58,139 --> 00:08:03,239 Python, Python will assign this name to 146 00:08:03,239 --> 00:08:05,579 the module. So this module will get this 147 00:08:05,579 --> 00:08:09,869 name. And the script actually, but it's, 148 00:08:09,929 --> 00:08:13,589 it's treated as a module. But if you 149 00:08:14,879 --> 00:08:18,648 import that module, so if I ran, I opened an 150 00:08:18,649 --> 00:08:22,719 interactive shell now. So from the account folder import 151 00:08:22,720 --> 00:08:25,538 [Author typing] 152 00:08:25,540 --> 00:08:30,269 acc, what happens is that you get the print 153 00:08:30,269 --> 00:08:32,759 function executed as well. But this time 154 00:08:32,759 --> 00:08:35,969 when you import a file, so when you 155 00:08:35,969 --> 00:08:38,309 don't execute it, like we did previously 156 00:08:38,309 --> 00:08:42,689 with python account\acc.py. When 157 00:08:42,689 --> 00:08:45,839 you put the module, the module gets the 158 00:08:45,839 --> 00:08:48,059 name of the script that it is written 159 00:08:48,059 --> 00:08:52,139 to. So in this case is acc, without the 160 00:08:52,139 --> 00:08:55,829 extension. Here, you also get the name 161 00:08:55,859 --> 00:08:59,309 of the package of the module. So 162 00:08:59,309 --> 00:09:02,189 packages are made of modules, and 163 00:09:02,219 --> 00:09:04,229 packages, like a folder and there you 164 00:09:04,229 --> 00:09:06,509 have several module, modules, or one 165 00:09:06,509 --> 00:09:08,729 module, and like we have, in this case. 166 00:09:09,719 --> 00:09:13,319 Our package here is account, so account 167 00:09:13,319 --> 00:09:15,029 and then it has this module and then 168 00:09:15,029 --> 00:09:18,592 this module has this class, the Account class. 169 00:09:18,593 --> 00:09:20,938 [No audio] 170 00:09:20,940 --> 00:09:22,499 So that was something I wanted to 171 00:09:22,499 --> 00:09:26,189 show you. Packages, modules and classes. 172 00:09:27,389 --> 00:09:32,399 Great, Now I'll exit of this Python shell and 173 00:09:32,489 --> 00:09:34,109 play around with this script a little 174 00:09:34,109 --> 00:09:37,709 bit. And what we did here, we printed 175 00:09:37,709 --> 00:09:40,859 out the objects namespace, and we didn't 176 00:09:40,859 --> 00:09:43,169 get the the actual balance as an output 177 00:09:43,169 --> 00:09:46,019 because we are not returning the balance 178 00:09:46,019 --> 00:09:49,499 here. But if you want to gather balance, 179 00:09:50,700 --> 00:09:55,080 you'd want to point to the balance that 180 00:09:55,080 --> 00:09:57,240 uses a dot notation to point to the 181 00:09:57,270 --> 00:09:59,970 object that holds the balance attribute. 182 00:10:00,545 --> 00:10:02,585 So let's see what we get this time. 183 00:10:02,586 --> 00:10:05,184 [No audio] 184 00:10:05,186 --> 00:10:07,530 And yeah, 1000. So that's the 185 00:10:07,530 --> 00:10:09,870 current balance. As you can see, you are 186 00:10:09,870 --> 00:10:12,360 able to access attributes of your 187 00:10:12,360 --> 00:10:15,810 object instance, using the dot notation. 188 00:10:16,050 --> 00:10:18,960 Good. Now, let's add these other methods 189 00:10:18,960 --> 00:10:22,560 there. And what can we do to a bank 190 00:10:22,560 --> 00:10:24,480 account? Well, we can withdraw money. 191 00:10:24,481 --> 00:10:26,638 [Author typing] 192 00:10:26,640 --> 00:10:30,600 And you always need to pass self in all 193 00:10:30,600 --> 00:10:33,090 of your methods, and have the parameter 194 00:10:33,090 --> 00:10:35,670 that applies to that particular method. 195 00:10:36,300 --> 00:10:38,790 In this case, when you withdraw some 196 00:10:38,790 --> 00:10:41,820 money, well, you have an amount in mind 197 00:10:41,850 --> 00:10:45,240 that you want to withdraw. So you pass 198 00:10:45,240 --> 00:10:49,200 amount there as a parameter. Now, just 199 00:10:49,200 --> 00:10:51,480 to be clear that you understand what I'm 200 00:10:51,480 --> 00:10:54,840 doing. What I'm doing here is, I have an 201 00:10:54,840 --> 00:10:58,470 initial balance that is being read from 202 00:10:58,770 --> 00:11:02,730 this file. So for this text file, and 203 00:11:02,730 --> 00:11:06,210 then I want to allow the user to apply 204 00:11:06,240 --> 00:11:08,580 operations or withdraw money from this 205 00:11:08,580 --> 00:11:11,460 balance. And once the user executes the 206 00:11:11,490 --> 00:11:14,460 withdrawal method, I want to update the 207 00:11:14,520 --> 00:11:17,520 balance number inside the object 208 00:11:17,520 --> 00:11:21,090 instance. And I don't want yet to write 209 00:11:21,090 --> 00:11:24,900 that balance to my balance file. So I 210 00:11:24,900 --> 00:11:27,060 just want to calculate something here, 211 00:11:27,060 --> 00:11:31,551 like self.balance equals to, 212 00:11:32,025 --> 00:11:35,880 self.balance, well, this is a withdrawal, so 213 00:11:35,910 --> 00:11:37,440 minus the amount that I want to 214 00:11:37,440 --> 00:11:40,050 withdraw. That means this will do self 215 00:11:40,050 --> 00:11:42,990 balance equals to 1000 minus 100, let's 216 00:11:42,990 --> 00:11:45,420 say, and I'll end up with an updated 217 00:11:45,450 --> 00:11:50,460 balance of 900, in this case. And then 218 00:11:50,490 --> 00:11:54,840 if I want, I can go ahead and open that 219 00:11:54,840 --> 00:11:58,800 file again here and then write these 220 00:11:58,830 --> 00:12:01,320 updated balance in that file. So that 221 00:12:01,320 --> 00:12:04,050 would be one solution. But a more 222 00:12:04,050 --> 00:12:06,480 constructive solution will probably be 223 00:12:06,480 --> 00:12:09,780 to have a specific method that combines 224 00:12:09,780 --> 00:12:13,440 the changes to the file, so you may want 225 00:12:13,440 --> 00:12:14,970 to withdraw some money, and then you 226 00:12:14,970 --> 00:12:18,090 deposit some money in the side the same 227 00:12:18,090 --> 00:12:20,040 session, and then you apply the commit 228 00:12:20,040 --> 00:12:22,470 method to save the changes. So this is 229 00:12:22,470 --> 00:12:26,010 the solution I want to go for. And here, we 230 00:12:26,010 --> 00:12:27,480 are done with the withdrawal methods. 231 00:12:27,510 --> 00:12:32,160 Now, deposit self and amount, again. 232 00:12:32,829 --> 00:12:36,555 So this is local variable, this is a local variable, 233 00:12:37,496 --> 00:12:39,491 they happen to have the same name, but 234 00:12:40,495 --> 00:12:42,522 they don't have to do matcher with each other. 235 00:12:44,479 --> 00:12:50,844 self.balance equals to self.balance 236 00:12:51,769 --> 00:12:55,023 towards the amount in this case. 237 00:12:55,314 --> 00:12:57,910 So let's see what we have this for. 238 00:12:58,546 --> 00:13:02,575 And I'll keep this instance object here 239 00:13:02,576 --> 00:13:05,008 that is being generated. So 240 00:13:05,010 --> 00:13:08,010 we are instantiating an object instance, 241 00:13:08,040 --> 00:13:10,050 in other words, and print out the 242 00:13:10,050 --> 00:13:12,960 current balance. And then I want to 243 00:13:12,960 --> 00:13:16,380 withdraw some money from my Account 244 00:13:16,410 --> 00:13:20,310 object, that means I apply withdrew to 245 00:13:20,310 --> 00:13:24,510 the Account object. And withdrawal takes 246 00:13:24,720 --> 00:13:27,120 two arguments. So the object 247 00:13:27,120 --> 00:13:28,710 instance, which is pass automatically, 248 00:13:28,710 --> 00:13:32,790 and then the amounts which could be 100. 249 00:13:34,410 --> 00:13:37,170 And once I do that, I want to print out 250 00:13:37,171 --> 00:13:39,178 [No audio] 251 00:13:39,180 --> 00:13:42,510 the updated balance. So let's see what 252 00:13:42,540 --> 00:13:49,020 is gonna do. Yeah, 1000. So an object instance is 253 00:13:49,020 --> 00:13:53,670 created, and you get 1000 printed out, 254 00:13:53,760 --> 00:13:55,860 because here you have 1000. And then you 255 00:13:55,860 --> 00:13:58,440 take 100 out of that, and so you end up 256 00:13:58,440 --> 00:14:03,780 with 900. Great. So if you run that 257 00:14:03,780 --> 00:14:07,200 again, you will notice that a new object 258 00:14:07,200 --> 00:14:11,160 instance is being created again. So you 259 00:14:11,160 --> 00:14:13,680 don't have 900 adds the current balance 260 00:14:13,680 --> 00:14:17,640 anymore, you have 1000 so that means you 261 00:14:17,640 --> 00:14:21,570 need to write the changes to the file. 262 00:14:22,320 --> 00:14:24,510 If you wanted to save those, that 263 00:14:24,510 --> 00:14:28,380 balance. And we can do that using a 264 00:14:28,560 --> 00:14:31,945 methods. Let's call it commit 265 00:14:33,930 --> 00:14:38,670 pass self there. And I don't think of any 266 00:14:39,060 --> 00:14:41,070 parameter that I need to pass to this 267 00:14:41,070 --> 00:14:43,590 commit method. Because we all we will 268 00:14:43,590 --> 00:14:47,010 work with all the existing variables 269 00:14:47,010 --> 00:14:49,920 there. So this would be something like 270 00:14:49,920 --> 00:14:55,949 with open well, we need to open the filepath, 271 00:14:56,144 --> 00:14:58,290 but we have a small problem and 272 00:14:58,290 --> 00:15:01,080 here because of file path variable 273 00:15:01,590 --> 00:15:03,810 now is just a local variable in this 274 00:15:03,840 --> 00:15:08,340 init method. So if we pass it here of 275 00:15:08,340 --> 00:15:10,830 this function with this method, we will 276 00:15:10,830 --> 00:15:14,310 not recognize it. So this is a bit 277 00:15:14,310 --> 00:15:17,340 different from the other, the backend 278 00:15:17,340 --> 00:15:20,220 script that we wrote, because here we 279 00:15:20,220 --> 00:15:23,850 have this parameter like we do in here. 280 00:15:24,420 --> 00:15:27,000 But in this case, this parameter, it was 281 00:15:27,000 --> 00:15:30,420 only used inside the init method, so, we 282 00:15:30,420 --> 00:15:35,070 didn't use it anywhere else. So there is 283 00:15:35,100 --> 00:15:38,191 a solution to that, I want solution 284 00:15:38,193 --> 00:15:40,800 would be to go ahead and pass 285 00:15:40,830 --> 00:15:43,920 another, let's call this path here. So 286 00:15:43,920 --> 00:15:46,230 another parameter to the commit methods, 287 00:15:47,070 --> 00:15:48,840 then, when we call the commit method, 288 00:15:48,840 --> 00:15:51,900 like we were doing with withdrawal, we'd 289 00:15:51,900 --> 00:15:56,070 have to pass the balance.txt. 290 00:15:56,130 --> 00:15:59,220 Actually this path to the commit method. 291 00:16:00,060 --> 00:16:02,160 While that would work, it's still not 292 00:16:02,160 --> 00:16:06,330 the best practice of Object Oriented 293 00:16:06,360 --> 00:16:09,720 Programming. So we need to seek for another 294 00:16:09,720 --> 00:16:13,140 solution. That would be to make this 295 00:16:13,140 --> 00:16:16,710 local variable instance variable. And to 296 00:16:16,710 --> 00:16:18,780 do that, you just do something like self 297 00:16:18,840 --> 00:16:22,710 and filepath equals to filepath. 298 00:16:22,711 --> 00:16:25,618 [No audio] 299 00:16:25,620 --> 00:16:28,740 And what this does is, so filepath, 300 00:16:28,740 --> 00:16:31,440 don't confuse this with this, this 301 00:16:31,440 --> 00:16:32,700 happened to be the same, but you can 302 00:16:32,700 --> 00:16:35,790 also write another thing here, like 303 00:16:36,210 --> 00:16:40,980 anything. So this is a parameter of the 304 00:16:40,980 --> 00:16:44,015 init function. So the balance.txt 305 00:16:44,016 --> 00:16:47,670 will go to this parameter. So the 306 00:16:47,670 --> 00:16:49,320 path of this file, we'll go to this 307 00:16:49,320 --> 00:16:51,360 parameter, and then you create this 308 00:16:51,390 --> 00:16:54,360 instance variable. And that instance 309 00:16:54,360 --> 00:16:58,020 variable will be equal to this path, but 310 00:16:58,020 --> 00:17:00,870 you can use this thing here, you can use 311 00:17:00,870 --> 00:17:03,900 it wherever you like inside your class. 312 00:17:04,470 --> 00:17:07,773 And in our case, we would like to use it in here. 313 00:17:07,774 --> 00:17:11,152 [Author typing] 314 00:17:11,153 --> 00:17:13,930 So we open the self.filepath. 315 00:17:15,207 --> 00:17:16,860 In the right method this time, 316 00:17:16,860 --> 00:17:19,530 because this time, we need to write 317 00:17:20,010 --> 00:17:23,610 or to overwrite in more accurately. To 318 00:17:23,610 --> 00:17:25,410 overwrite the current balance, which 319 00:17:25,410 --> 00:17:27,270 should be of this one, for example, we 320 00:17:27,270 --> 00:17:29,527 want to overwrite that to this file. 321 00:17:29,528 --> 00:17:32,130 [Author typing] 322 00:17:32,132 --> 00:17:35,040 So as file, this is just a temporary variable, 323 00:17:35,580 --> 00:17:42,530 file, and file.write and self.balance. 324 00:17:42,531 --> 00:17:50,698 [No audio] 325 00:17:50,700 --> 00:17:52,650 Good, let's try this out. 326 00:17:52,651 --> 00:17:59,632 [No audio] 327 00:17:59,634 --> 00:18:03,000 And we didn't get any update in the 328 00:18:03,000 --> 00:18:06,210 file here, because I forgot to actually 329 00:18:06,210 --> 00:18:09,690 call the commit method. So once we 330 00:18:09,720 --> 00:18:12,090 withdraw money, let's bring that out in 331 00:18:12,090 --> 00:18:16,153 the console, and then account.commit, 332 00:18:17,308 --> 00:18:20,857 or without passing any arguments 333 00:18:20,940 --> 00:18:22,980 exclusively, because implicitly, we're 334 00:18:22,980 --> 00:18:27,690 passing the object thesis. Good. We got 335 00:18:27,690 --> 00:18:32,160 an error there invalid literal for integer with base 10. 336 00:18:32,880 --> 00:18:37,903 So this would be the integer in line 6, 337 00:18:39,426 --> 00:18:44,140 here in. So the value that is being read by 338 00:18:44,141 --> 00:18:47,010 this method is not converting to an integer. 339 00:18:48,270 --> 00:18:50,460 And the reason is that this value has 340 00:18:50,460 --> 00:18:54,090 disappeared from balance.txt. And 341 00:18:54,120 --> 00:18:56,190 this is reading an empty string. So 342 00:18:56,250 --> 00:18:58,320 this is an empty string that is being 343 00:18:58,320 --> 00:19:01,950 read from this file, then you try to 344 00:19:01,950 --> 00:19:04,710 convert that and doesn't work. Now the 345 00:19:04,710 --> 00:19:07,440 reason that we got this empty string, an 346 00:19:07,440 --> 00:19:11,040 empty file here is when we got the 347 00:19:11,280 --> 00:19:16,200 previous error. So here, right argument 348 00:19:16,200 --> 00:19:19,050 must be string, not integer. So what 349 00:19:19,050 --> 00:19:23,186 Python did was it opened the file with 350 00:19:23,188 --> 00:19:25,560 the method but then it wasn't able to 351 00:19:25,590 --> 00:19:28,590 actually write a value inside that file. 352 00:19:28,590 --> 00:19:34,380 So the file ended up empty. So lets me 353 00:19:34,380 --> 00:19:40,035 write the initial value there, then 354 00:19:40,528 --> 00:19:43,493 now we should be able to draw that again. 355 00:19:43,494 --> 00:19:47,567 [No audio] 356 00:19:47,569 --> 00:19:50,774 And yep, now we have the updated value. 357 00:19:52,293 --> 00:19:55,153 So again, what we did is we created the 358 00:19:55,154 --> 00:19:57,000 object instance and then we print in the 359 00:19:57,000 --> 00:20:00,300 current balance which was read by from 360 00:20:00,300 --> 00:20:02,970 this file, which was 1000 at the 361 00:20:02,970 --> 00:20:06,120 beginning, and then withdrew 1000, so 362 00:20:06,120 --> 00:20:09,390 the balance was updated. Then we commit 363 00:20:09,390 --> 00:20:13,412 those changes to this file, so we get 364 00:20:13,414 --> 00:20:16,590 900 there. Now, same thing if you want 365 00:20:16,800 --> 00:20:23,670 to add, let's say 200, and deposit. 366 00:20:23,671 --> 00:20:26,368 [Author typing] 367 00:20:26,370 --> 00:20:28,078 Let's see if this will work. 368 00:20:28,080 --> 00:20:30,387 [No audio] 369 00:20:30,389 --> 00:20:33,150 Yeah, this was printed out. Let's check file. 370 00:20:33,600 --> 00:20:37,770 Yeah, so this is our new balance. And 371 00:20:37,770 --> 00:20:40,440 yeah, I hope this clarifies a few 372 00:20:40,470 --> 00:20:42,510 concepts about Object Oriented 373 00:20:42,510 --> 00:20:45,900 Programming. If you are creative, you 374 00:20:45,900 --> 00:20:48,450 can go ahead and expand the script. So 375 00:20:48,450 --> 00:20:50,490 you can add more methods there, and 376 00:20:50,490 --> 00:20:53,310 maybe make this more of a real life 377 00:20:53,340 --> 00:20:57,360 application. So great. And this is about 378 00:20:57,360 --> 00:20:59,220 this lecture and in the next lecture, 379 00:20:59,220 --> 00:21:02,671 I'll talk about inheritance. And we will be 380 00:21:02,672 --> 00:21:05,406 expanding the script by applying inheritance 381 00:21:05,917 --> 00:21:10,037 in there. So talk to you in the next lecture.