Well in the previous lectures I mentioned that I'll be creating a program,
an application where we'll be handling a sort of bank account so that we'll have
a balance, and then we'll be applying some operations such as
withdraw money from this bank account object, and deposit money as well. I will
be storing the values of the balance in a simple text file. So that's not one of
ten programs we're building in the course. This is just to get you familiar with
classes and to explain them a little bit more, so I'll do some housekeeping here.
So let me create a folder here, and put all these existing files in this folder.
So we have the previous files in here and I'll create a new folder now let's call
this account, and it is folder I'll have my Python script, so let's call it like
this, so on the account we have this Python file and we also need this text
file where we'll be saving the current balance of our bank account, so this will
be a trivial application because in real life you don't want to save the bank
account information, the balance in a text file. In real life you do want to
save that in a real database such as PostgreSQL for instance, but for the
sake of practicing object-oriented programming we will just save the
balance value in this file, so this is our storage. And also for the sake of
simplicity we'll be working only on one single account so our program will be
able to process one account object because if you have multiple accounts
then the solution could be different from this, so you'd probably have to
think about having a table and maybe a database would be necessary here.
So let's think about our personal account. You could use this program for
your personal notes, say you may want to keep track of your balance and you can
use this Python program, and now the object here is the bank account and about
this object we should start thinking about what attributes this bank account object
should have as an initial state. So we talked about this minimal object and
a bank account would have of course an initial balance, so let's start to create
this account object and call it account. You can call it whatever you want, but
I'll call it account, so in itis the first function we want to define and we
will pass self in there, and let's keep it like that for a moment, and we said
our bank account object will have balance attribute, and in our case we have
this balance dot txt and let's create an initial value there, and to create
an object instance we need to grab this value and pass it to our init function
so that we construct this minimal object. And because we have a file, what we need
to do is we need to read this number from this file, and naturally what you'd
want to pass here is some parameter like file path, so this is how I'm going to
call that, and now if you remember in the bookstore application in the backend
script, here we also passed a parameter to the init function, and then we created a
connection object out of this database file. In this particular case what we
want to create is an integer, so a number of this file, out of this file path.
So you could do something like with open, so we're doing a file handling
with method. With open file path. And this is a text file, so we need
to go with r mode which means read, so we're opening the file path of the
file, this file in read mode as a file. So this is just a temporary
variable that exists in this with statement, and then we need to read from
this file, so something like file dot read. Yeah, that's it. This will read the file,
but you you're not storing that anywhere, so what you want to do is say
self and balance equals to file dot read. So what that will do it will save the
value inside the text file in this instance variable, so this is an instance
variable, that is how it's called. Actually this is the instance variable and this
is the object, so the account object. Good. This is almost good and we also need to
add the integer function that because this will be read as a string by default so
we need to convert that value to an integer.
Yeah, that should work and that should construct the object for us, the account
object with a balance attached to it. And before going and creating the like a
withdraw method here, and then deposit methods, I'd prefer to call an instance
of this class first. So how to do that here? Let's say
store that in that account variable so this will be the object, the object that
will be created out of this blueprint, the account blueprint, and there
you need to pass this argument, the argument for this parameter
which is balance dot txt. Great, and for self Python will automatically pass the
account object instance. Good, now this will create an object instance and let me
print out that object here, so we see some output in action. Python and that's
inside account folder and acc.py. We got an error. No such file or directory for
balances dot txt, and the reason to that is that we need to pass the account
directory there as well. Yeah, this time it's working, so we had to pass the
directory there because we are in this folder in the demo folder and you need
to pass the account folder first and then the points to the balance of txt.
This is the output, so it says main dot account object, so the account object of
the main module, and what main is, is when you execute a script like we did here,
so you point to the script with Python, Python will assign this name to the
module, so this module will get this name. And the script actually, but it's
treated as a module, but if you import module, so if I ran, I open an interactive
shell now, so from the account folder import acc what happens is that you get
the print function executed as well, but this time when you import that file, so when
you don't execute it like we did previously with Python account acc.py,
when you import the module, the module gets the name of the script that
it is written to, so in this case is acc without the extension.
And here you also get the name of the package of the module, so packages are
made of modules and a package is like a folder and there you have several modules,
or one module like we have in this case, and our package here is
account, so accountant then has this module, and then this module has this
class the account class, so that was something I wanted to show you.
Packages, modules, and classes. Great, now I'll exit this plan shell and play
around with this script a little bit. And what we did here, we printed out the
object to namespace and we didn't get the actual balance as an output because
we are not returning the balance here. But if you want to get the balance, you'd
want to point to the balance and that uses a dot notation to point to the
object that holds the balance attribute. So let's see what we get this time.
Yeah, 1 000, so that's the current balance. As you can see you are able to
access attributes of your object instance using the dot notation.
Good, now let's add these other methods there and what can we do to that
bank account? Well we can withdraw money. And you always need to pass self in
all of your methods, and then the parameter that applies to that
particular method. In this case when you withdraw some money, well you have an
amount in mind that you want to withdraw so you pass amount there as a parameter.
And now just to be clear that you understand what I'm doing, what I'm doing
here is I have an initial balance that is being read from this file so for
this text file, and then I want to allow the user to apply operations,
so withdraw money from these balance, and once the user executes the withdraw
method, I want to update the balance number inside the object instance, and I
don't want yet to write that balance to my balance file so I just want to
calculate something here like self balance equals to self balance, this
is a withdraw so minus the amount that I want to withdraw. That means this will
do a self balance equals to one thousand minus one hundred, let's say and I'll end
up with an updated balance of nine hundred in this case, and then if I want
I can go ahead and open that file again here and then write these updated
balance in that file, so that would be one solution, but a more constructive
solution would probably be to have a specific method that commits the changes
to the file. So you may want to withdraw some money, and then you deposit some money
the money inside the same session and then you apply the committee method to
save the changes. So this is the solution I want to go for. And yeah, we are done with
the withdraw methods. Now deposit self and amount again, so this is a local
variable. This is a local variable. They happen to have the same name, but they
don't have to do much with each other. Self balance equals to self balance
plus the amount in this case, so let's see what we have this far, and I'll keep
this instance object here that is being generated so we are instantiating an
object instance in other words, and print out the current balance, and then I want
to withdraw some money from my account object. That means I apply withdraw
to the account object, and withdraw takes two arguments, so the object instance which
is passed automatically and then the amount which could be 100. And once I do
that I want to print out the updated balance, so let's see what it's gonna do.
Yeah, 1000 so an object instance is created, and you get 1000 printed out because here
you have 1000 and then you take 100 out of that and so you end up
with 900. Great, so if you run that again you will notice that a new object
instance is being created again, so you don't have 900 as the current balance anymore.
You have 1000, so that means you need to write the changes to the file
if you wanted to save those, that balance. And we can do that using a method,
let's call it commit, pass self there and I don't think of any
parameter that I need to pass to this commit method because all we will
work with are the existing variables there. So this would be something like
with open. Well we need to open the file path, but we have a small problem here.
Because the fault path variable now is just a local variable in this init
method, so if we pass that here this function with this method will not
recognize it, so this is a bit different from the other, the backend script that
we wrote because here we have this parameter like we do in here, but in this
case this parameter it was only used inside the init methods so we didn't use
it anywhere else. So there is a solution to that.
One solution would be to go ahead and pass another, let's call this path here,
so another parameter to the commit method. Then when we call the commit method
like we were doing with withdraw, we'd have to pass the balance dot txt.
Actually this path to the commit method. While that'll work, it's still not the
best practice of object-oriented paradigm, so we need to seek for another solution,
and that would be to make this local variable an instance variable, and to do
that you'd do something like self and file path equals to file path and
what this does is, so file path, don't confuse this with. This this happened to
be the same, but you can also write another thing here like anything so this
is the parameter of the init function so the balance of txt a will go to this
parameter, so the path of this file will go to this parameter, and then you create
this instance variable, and that instance variable will be equal to this path, but
you can use this thing here you can use it wherever you like inside your class
and in our case, we would like to use it in here. So we open the self dot filepath
in the write method this time because we this time we need to write
or to overwrite in more accurately, to overwrite the current balance which
should be this one for example. We want to overwrite that to this file. So as file,
this is just a temporary variable file, and file dot and self dot balance.
Good, let's try this out, and we didn't
get any update in the file here because I forgot to actually call the commit
method and so once we withdraw money let's bring that
out in the console, and then account dot commit without passing any argument
explicitly because implicitly we are passing the object instance, good.
Let's see what we get this time, and we got an error there. Invalid literal for
integer with base 10, so this would be the integer in line six here.
So the value that is being read by this method it's not converted to an integer.
The reason is that this value has disappeared from balance dot txt, and this is
reading an empty string, so this is an empty string that is being read from this
file, and then you try to convert that and it doesn't work. Now the reason that
we got this empty string, and empty file here is when we got the previous error,
so here right the argument must be string not integer, so what Python did was it
opened the file with the method, but then it was unable to actually write a value
inside that file, so the file ended up empty.
So let me write the initial value there, and then now we should be able to
run that again, and yeah now we have the updated value, so again what we did
is we created the object instance and then we created the current balance
which was read from this file which was 1000
in the beginning, and then we withdrew 100 so the balance was updated.
Then we commit those changes to this file, so we get my 900 here.
Now same thing if you want to add let's say 200 and deposit, let's see if
this will work. Yeah, this was printed out. Let's check the file, yeah, so this is our
new balance, and yeah I hope this clarifies a few concepts about
object-oriented programming. If you are creative you can go ahead and expand
this script, so you can add more methods there and maybe make this more of a real
life application. So great and this is about this lecture, and in the next
lecture I'll talk about inheritance, and we'll be expanding this script by
applying inheritance in there. So talk to you in the next lecture.