Before we move on to a new lesson in our object oriented program.
And I wanted to give you one more example
so that you can really understand what it is that we're doing.
So let's create a different class. OK?
Let's do something related to cybersecurity.
Let's say, for example, we wanted to create a class called user.
OK?
And the user accounts will have different attributes like the username,
the role. OK? And then let's also create a method.
Say for example, what can the user do? The user can log in?
OK? So
let's do that. So I'm going to remove all of this code
and at the very top, I'm just gonna say uh user
uh user class. OK? So
how do we create our user? We say class
and then user colon
and now what do we do?
We have to define the attributes
that will be associated with each user account.
So I'm gonna say define underscore in it
underscore
and now in brackets, what do we have, we have self because we have to
make a reference to each individual user account.
So I'm gonna say self and then the attributes will
be username and let's just go with the wall.
OK? One more. Just
hold on and that's it.
What's going to be the next thing to do?
The next thing will not be to assign variables to each of our attributes. So
I'm gonna say self dot username
equals username
pretty straightforward.
And then self dot role should be equal to what role
there it is.
OK? So we've defined the attributes now, what about the methods?
Let's create just one method called log in.
OK? So user can actually log in. So
I'm gonna say def and then log in and then in brackets,
what's it going to be? It's going to be self,
always self colon.
And now what should happen once a user has logged in?
Let us print,
hey, welcome message.
So I'm going to use the F string
codes and then we can say welcome and then
let Python fill in the rest of the information right there. So welcome
and then
uh self username
and there it is,
that's all we need to do.
And now last but not least
let us create
an object basically a user account and then let's call our method,
right? So
I'm gonna say admin
underscore user, this can represent our first account which will be for an admin,
it's gonna be equal to what user.
And now in brackets,
we have to fill in the values of the attributes which are username and
the username. In this case, right now is gonna be Alex. OK?
User name is Alex, sorry in codes
Alex. And then the role of Alex is obviously that of
a administrator. So
admind
administrator.
OK?
And then finally let us call our method. So it's gonna be admin
underscore user dot log in
brackets.
And if this program works,
it should say something like welcome Alex.
Let's run
and there you go.
Welcome Alex.
So this has been another example of how to create a class,
how to define the attributes
for each object in that class. And then we also created a method
for each individual object
and then we of course created an actual object and
then finally called the method associated with the object.
So hopefully you now have a better understanding
of.
Oop.
Thank you for watching the video. I'll see you in the next class.