So what we're going to do right now is we're going
to create our code centered around the previous example I gave you
regarding the library, the books and the attributes and methods
associated with each book. So
the first thing we need to do is to define
the class book. So
I'm gonna say class
book
and this is how we would create the class called book very straightforward.
Now, the thing is
we need to
indicate
the attributes
that are going to be associated
with every object of the book.
OK?
So to do this, I'm going to say the fine
and now
in it
underscore underscore and now inside of the brackets,
this is where
we are now going to indicate
what attributes are gonna be associated with each book.
You typically start off with something called self. OK? This is very, very common
in object oriented programming. OK? So
self is a way to refer to each individual object
of the class we're dealing with.
In this case right now, it's going to be each individual book.
So you always want to set off itself and now the actual attribute.
So I'm gonna go with title
also,
and let's add one more, let's say genre,
OK? Why not genre
call on
so far? So good.
OK?
Now we'll have to assign variables to each of our attributes.
So I'm gonna start off by saying self dot title.
You see how this works right now,
self dot title equals title.
And now can you guess the next one, self
dot author
equals author
and then again, self dot
genre
equals to genre
so far? So good.
I'm gonna add one more attribute.
It's not technically an attribute that we've defined thus far.
But it it will be useful once we start creating the methods
that will apply to the book. OK?
And that is we want to indicate whether or not
a book is available to be borrowed or not.
So
I am going to say self dot
available right now, let's say self is
OK,
is underscore available,
OK?
We want to initialize this to be true at the very beginning. So
at the very beginning,
all books are available to be borrowed.
OK?
Now
we've defined the attributes title author
genre.
We've also added one of the methods,
one of one of the parameters that we're going to use in
our methods that is whether or not the book is available.
So now let us define the actual methods themselves.
And the one that we're going to be dealing with here
is going to be the borrow. So I'm gonna say borrow
and now in brackets self.
OK.
We are creating a method
that we can apply to the object book. So
let us say that if the book is indeed available,
what should happen?
So I'm gonna say if
self self referring to what the book
is in fact available,
what do we do
remember that once the book is available
to be borrowed,
we will need to update
the records in the library to indicate that the book
is no longer available because now it's just been borrowed,
right?
So
we're gonna say
self
uh dot is available
should not be equal to what? False?
Because it's no longer available.
OK? Look at line 11. Again, this goes, this is very, very important. OK?
Right here, we're doing that for the very first time.
Somebody wants to borrow this book or maybe not for the first time,
but the book is currently available.
So what should happen?
We should say that
we should update the records to indicate that the book is no longer available
and then
let us print out something. OK,
let us say print.
And now in brackets, I'm gonna use the F string
because I want to reference the actual title of the book that's been borrowed.
So I'm gonna open up my collar braces
and I'm gonna say self dot What's the title?
OK. And now we can type in
whatever it is we type in has been
boiled.
OK?
At the closing quotes
and there it is
So
two things should happen if the book is available to be borrowed.
First of all, we need to update the records to indicate that the book has no
longer available available because it's now been borrowed.
And then we should print out the message saying
the book has been borrowed.
What is the else statement here for? What are why we indicating
we didn't indicate
because
online 11 where it says if self is available
right now at this stage,
the program is assuming that the book is currently available.
What if in a scenario where
the library user wanted to borrow a book
that wasn't actually available in the first place?
That's what the else statement here is going to represent.
So else if the book wasn't available initially, just simply print,
print uh
print
and then
we can just say sorry,
this book is not currently
available
and I
forgot to put this in code,
sorry, this book is currently
not available.
Of course,
we can also use the F string and self title to reference the actual name of the book.
But
that's not necessary.
We're just gonna say sorry, this book is not currently available.
OK?
Awesome.
Now
let us define another method
which would be to return the book
so we can borrow the book,
but we can also return it. So
I'm gonna say
define return underscore book self.
So we've given the function of the method
called return underscore book and then self.
So in
this case, right now, what should happen
if the book has been returned?
Now, we're gonna say self dot is available should now be equal to what true?
Because the book is now available.
And let's go ahead right now and print a message
and say,
let's also reference. So I'm going to say F string
and then
the name of the book has been returned.
All right.
So
let us try
this. Let's put it into action. OK? So I'm gonna come all the way down here,
let's say book
underscore A
equals
now to the actual object called book.
And now inside,
let us provide the name the title of the book, first of all.
So let's say Python 101.
OK. What's next the author, right? So let's call the author
Alice.
OK? And then what's the genre gonna be?
Uh let's say programming, right? Programming.
And
now let us create another object.
This object will be represented by book
underscore B
equals. And now we get book
and now in brackets,
let's say cyber
201. OK. And then
the author here is going to be Jack.
And then what's going to be the genre,
let's just say tech technology, right? Technology should be the genre
and there it is.
So
gonna come all the way down here
and now
let us call the methods.
OK.
We have everything in place right now.
Let us call our methods which are of course, what b
and then return.
So
I can say book, first of all
underscore
A
dot
Borrow
in brackets.
All right.
So, first of all, we're gonna borrow the book
and then
let me run the program
to use it right now. It says, ok, Python 101 has been borrowed
because over here it's going to indicate online 11.
It's going to indicate at the very beginning that, ok, the book is available,
it's now been borrowed.
So we'll have to update the records to say it's false. And now we're gonna say
the book has been borrowed.
But now
at this current stage, the book isn't available, right?
So let us target the L statement. It should now print,
sorry, this book is not currently available. If I want
the method again, what if somebody else now comes in
and wants to borrow the book
that's already not available? What's gonna happen?
I'm gonna run the program
and now you say it, say sorry, this book is not currently available.
Ok?
But
what if,
what if
I now return the book?
What's he gonna say?
It's gonna say Python 101 has been returned.
And now
if I run
the return method again, I'm sorry, the borrow method again
one more time
was it gonna say
it's gonna say Python 101 has been browed.
So you can see right now
it works perfectly fine. So
just to give you a very quick recap first of all, we had to create a class called book.
And now inside we have to define the attributes
associated with each book.
And we did that by saying, define on the score and the score in it on the score and then
the parameters of the attributes in brackets. And then
we assigned each attribute to a value. So self dot title
will be equal to title. Self dot author will be equal to author.
Self dot journal will be equal to genre.
And then we also created a method in here that
will indicate whether or not we can borrow the book.
So this is basically a boolean attribute.
We said, OK, self dot is available at the very beginning should be equal to true.
And now we created two different methods. The first one here would be the
bole.
So what happens when a book is borrowed?
If it was initially available,
we'll update the vehicles to say self is available is no longer available.
So it's false.
And then we should print out the title that says
sorry that this book has been borrowed.
However, else if the book was already borrowed initially,
we should just simply print out sorry.
This book is not currently available.
And then the second method what happens when the book is returned?
We should now update our records yet again to
indicate that self is available is now true.
And then we can print out the name of the book saying it has been returned.
And finally,
we learned how to create individual objects in individual books.
We can simply say book underscore A equals book. And then
in brackets, we have to fill in the values for
the
attributes.
And then we created another book, book B which is Cyber to One Jack Technology.
And then we called the Methods
and that's pretty much it. So thank you for watching the video.
I will see you in the next class.