Well, in the previous lectures, I
mentioned that I'll be creating a
program and application where we'll be
handling, sort of bank account. So that
will have a balance. And then we will be
applying some operations such as
withdraw money from his bank account
object, and deposit money as well. I will
be storing the value, so the balance in a
simple text file. So that's not one of
10 programs, so we're be building in the
course. And 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.
[Author typing]
And put all these existing files
[No audio]
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 in this 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 these text
file where we'll be saving the current
balance of our bank account. So this
will be a trivial application, because
you realize you don't want to save the
bank account information and the
balance in a text file. Here you like, you'd
want to save that in 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 lists. 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. So 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 of 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,
init, here's the first function we want to
define, I would pass self in there.
Anyhow let's keep it like that for a
moment. And we said, our bank account
object will have a balance attribute.
And in our case, we have this balance.txt.
And let's create an initial value there. And now,
to create an object instance, we need to grab this
value and pass it to our init function,
so that we can start 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
pass 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 out of this file,
out of this filepath. So you could do
something like with open so we're doing
file handling with, the with methods
with open filepath. And this is a text
file, so we need to go with our mode,
which means to read. So we're opening
the file pass through the file, this file, a
read mode as an as a file. So this is just
a temporary variable that exists in this
with a statement. And then we need to
read from this file. So something like
file.read, and yeah, that's it. This will
read the file, but you're not
storing that anywhere. So what you want
to do is, say self.balance equals to
file.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
there, because this will be read as a
string by default. So we need to convert
that value to an integer. And yeah, that
should work and that should construct
the object for also the Account object
with a balance attached to it. And
before going and creating the, like a
withdrawal methods here, and then
deposit methods, I'll prefer to call an
instance of this class first. So how to
do that here? Let's say store that in the
account variable, so this will be the
object, the object that will be created
out of these blueprints, the Account
blueprint. And there you need to pass this
argument, the argument for this
parameter, which is balance.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, that's inside the
account folder, and acc.py. We got an
error, no such file or directory for
balance.txt. And the reason to that,
is that we need to pass the account
directory there as well. So
[No audio]
anyhow 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 point to the balance.txt. And
this is the output. So it says main.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 pointed 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,
it's treated as a module. But if you
import that module, so if I ran, I opened an
interactive shell now. So from the account folder import
[Author typing]
acc, what happens is that you get the print
function executed as well. But this time
when you import a file, so when you
don't execute it, like we did previously
with python account\acc.py. When
you put the module, the module gets the
name of the script that it is written
to. So in this case is acc, without the
extension. Here, you also get the name
of the package of the module. So
packages are made of modules, and
packages, like a folder and there you
have several module, modules, or one
module, and like we have, in this case.
Our package here is account, so account
and then it has this module and then
this module has this class, the Account class.
[No audio]
So that was something I wanted to
show you. Packages, modules and classes.
Great, Now I'll exit of this Python shell and
play around with this script a little
bit. And what we did here, we printed
out the objects namespace, and we didn't
get the the actual balance as an output
because we are not returning the balance
here. But if you want to gather balance,
you'd want to point to the balance that
uses a dot notation to point to the
object that holds the balance attribute.
So let's see what we get this time.
[No audio]
And yeah, 1000. 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 a bank
account? Well, we can withdraw money.
[Author typing]
And you always need to pass self in all
of your methods, and have 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. 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 or withdraw money from this
balance. And once the user executes the
withdrawal 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, well, this is a withdrawal, so
minus the amount that I want to
withdraw. That means this will do self
balance equals to 1000 minus 100, let's
say, and I'll end up with an updated
balance of 900, 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 will probably be
to have a specific method that combines
the changes to the file, so you may want
to withdraw some money, and then you
deposit some money in the side the same
session, and then you apply the commit
method to save the changes. So this is
the solution I want to go for. And here, we
are done with the withdrawal methods.
Now, deposit self and amount, again.
So this is local variable, this is a local variable,
they happen to have the same name, but
they don't have to do matcher with each other.
self.balance equals to self.balance
towards the amount in this case.
So let's see what we have this for.
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 withdrew to
the Account object. And withdrawal takes
two arguments. So the object
instance, which is pass automatically,
and then the amounts which could be 100.
And once I do that, I want to print out
[No audio]
the updated balance. So let's see what
is 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 adds 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
methods. 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 we all we will
work with all the existing variables
there. So this would be something like
with open well, we need to open the filepath,
but we have a small problem and
here because of file path variable
now is just a local variable in this
init method. So if we pass it here of
this function with this method, we 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 method, so, we
didn't use it anywhere else. So there is
a solution to that, I want solution
would be to go ahead and pass
another, let's call this path here. So
another parameter to the commit methods,
then, when we call the commit method,
like we were doing with withdrawal, we'd
have to pass the balance.txt.
Actually this path to the commit method.
While that would work, it's still not
the best practice of Object Oriented
Programming. So we need to seek for another
solution. That would be to make this
local variable instance variable. And to
do that, you just do something like self
and filepath equals to filepath.
[No audio]
And what this does is, so filepath,
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 a parameter of the
init function. So the balance.txt
will go to this parameter. So the
path of this file, we'll 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.
[Author typing]
So we open the self.filepath.
In the right method this time,
because this time, we need to write
or to overwrite in more accurately. To
overwrite the current balance, which
should be of this one, for example, we
want to overwrite that to this file.
[Author typing]
So as file, this is just a temporary variable,
file, and file.write and self.balance.
[No audio]
Good, let's try this out.
[No audio]
And we didn't get any update in the
file here, because I forgot to actually
call the commit method. So once we
withdraw money, let's bring that out in
the console, and then account.commit,
or without passing any arguments
exclusively, because implicitly, we're
passing the object thesis. Good. We got
an error there invalid literal for integer with base 10.
So this would be the integer in line 6,
here in. So the value that is being read by
this method is not converting to an integer.
And the reason is that this value has
disappeared from balance.txt. And
this is reading an empty string. So
this is an empty string that is being
read from this file, then you try to
convert that and doesn't work. Now the
reason that we got this empty string, an
empty file here is when we got the
previous error. So here, right argument
must be string, not integer. So what
Python did was it opened the file with
the method but then it wasn't able to
actually write a value inside that file.
So the file ended up empty. So lets me
write the initial value there, then
now we should be able to draw that again.
[No audio]
And yep, now we have the updated value.
So again, what we did is we created the
object instance and then we print in the
current balance which was read by from
this file, which was 1000 at the
beginning, and then withdrew 1000, so
the balance was updated. Then we commit
those changes to this file, so we get
900 there. Now, same thing if you want
to add, let's say 200, and deposit.
[Author typing]
Let's see if this will work.
[No audio]
Yeah, this was printed out. Let's check 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 the 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 will be
expanding the script by applying inheritance
in there. So talk to you in the next lecture.