Hey everybody welcome to the next video.
So in this video we're gonna be going over something called a class.
Now what a classes allows you to structure us and let you build a structure or a blueprint for an object
that you want to use.
Now this object can do anything you want it to as long as you program it to do so.
So we're going to do a quick example here.
So if we create a new file right we'll take we'll name this person got P Y so we've got a new class
or a new file or new source python file in the same directory now similar to how we define a function
with Def what we want to do is we want to define a class.
So you're going to class and then the name of the class.
In this case we're going to do person.
We'll see how it looks very similar to a function that we've done over here.
So def main we're just replacing this def with class and then giving it a name.
Now with classes you tend to keep the class name to the same as the person now.
In this case we we want to use.
We want to create an object called person that has a few bits of information.
We want it to hold the person's name their age and then the title.
And we want it to provide some sort of output in a structured way.
Now we could write this all by hand.
However if we want to create 10 people a thousand a million people in our application that means we'd
have to rewrite that same code multiple times.
Now this is a good way for us to reuse the structure of something.
But just changing the data that's inside of it to be in its own instance.
So this to make more sense in a second so we want to do as every class should have something called
a constructor.
It's not required but when you create a class over here.
So in this case our main method we're going to create an instance of that class or a new version of
it or a copy of it.
We want it to have some default values.
So to create an a constructor in Python you can do def which is the same thing we use for functions
underscore underscore in it that is going to be our constructor.
So in it is a special term that's used for the initialize.
So that's also called the instruct the constructor.
Now every inside of a class every function needs to take something called self.
So what self is it refers to the instance of this object or this particular variable.
So what we'll do is I just want to show you quick thing here.
So if I do print this would make more sense of just the second print made it.
Right.
So this does nothing.
If we come in here and we run main nothing happens because there's nothing there.
So we'll just do print in May.
Right.
So inmates.
So we're not getting anything.
However we want to create a person.
So let's say we'll take we'll get this an idea just for sake of I'll explain this in just a second here.
So we'll print out the I.D. here.
So we'll do I.D. a keyword.
So we can't use that.
So t temp I.D..
Right.
Just for sake of showing you how this works.
So what we do is when we want to use a classmate to import it right we want to do is do from person
import person.
So from the name of the file in this case it's person import whatever the class name is.
In this case it's a person.
Right.
So we've got our person.
So we want to do Person 1 is equal to person.
So you just see how this looks similar to a function.
However we're calling it.
So this calls a constructor so whatever we pass here is what gets dumped into here.
Now self is already added by default.
So we don't have to worry.
We don't have to send self or anything here.
But when we want to do a send an I.D. So we'll get that one right but if we want to create a bunch of
these.
Right.
So if we want to create 2 3 4 2 3 4.
So if we run this now we should get made it.
And then the I.D. of each person so we can see here it's made it one two three four.
So we've done that without having to rebuild we didn't have to rewrite this entire thing.
Now what if we want to store the title the name of the person where they work maybe their Social Security
number their phone number.
So what we can do is because each time we do this we're creating a new copy of person with the data
that we want every time we reference person 1 person to person 3 person for they are their own copy.
So the data inside of them is stored with just them.
It's not they're not shared.
So Person 1 does not share anything with person to their exact copies of each other initially.
And then when we change the data they stay separate hopefully that that understand that that makes a
little bit more sense.
So let's say we want to pass in the person's name and age right.
So what's important here is we do self name is equal to name and self-doubt age.
Age now self again references its current the current copy of the person object.
So in this case person 1 has its own self which is its own value.
Now we do self dot the value of the variable name because we want the variable to be specific to this
instance of the object.
So when we set we taken the name and then we assign it to the local instance of this person to name.
So that way when we wrote want to reference the name of the person we would do self name instead of
just name.
So what if we want to do title here.
Right.
So do self got title and most said it currently to nothing because we don't require a title let's say
so next thing we want do is do def get title so.
So there's in classes there's tends to be something called getters and setters.
Now a getter in this case is to get followed by the variable name.
So in this case title.
So what we're gonna do is we're in the B return self title that's it.
So a getter Well essentially you just want when somebody uses a getter and they say get title they want
to just get the title they don't want anything else just title and return is a good way to kick back
to the app to the calling application and then we're returning a value.
So title which would just return a String it doesn't return anything special just just the string itself.
So if we came in here we'll set title to test.
Right.
Actually we'll set it to
the A we'll set it to the name for now.
Just so I can show you the difference between the two.
Right.
So we've got our person.
Right.
Now if we run this and we remove that print statement nothing should capital that.
What
Oh that's right.
Because we're adding the idea here when it's no longer needed.
However we need to add the name the age.
So if we add the name here as Brandon twenty six and then we do
Todd twenty five
do Tory twenty four and we'll do the show 30.
Right.
We'll do it here is we want to do this.
We're going to create one called get stats.
Now what we're doing here is this is just a quick way for me to show you what we're going to accomplish
here.
So we're going to return a String.
We want this to just basically give us all the information we want to get the name of the person the
age.
So this all should he should be familiar.
And then the title of the person right so yourself name self.
Dark Age self.
The title.
So when we come through here you can do print
person 1 get stats so gets.
Yep.
So if we print this we can see in Maine and then we've got Person 1 here which is Brandon twenty six
and then the title we set to the name temporarily so we'll do
the first one right.
So let's go ahead and do this a few more times.
We'll do this two times just for sake of simplicity so we'll get rid of these two
right.
So we go person to person.
Nice to see we have set Brandon 26 Michele 30.
We've set them separately.
However if we look here we see person 1 Brandon 26.
Title Brandon person 2.
Michelle 30.
Michelle.
So we see we have two completely separate copies and they're kept separate.
So everything's done on their own.
Now with that get we can do a set or two.
We can do set title and this works just as we did before we have self because that's required.
And then we pass it a title.
Now what we do here is to we yourself that title since we're referring to the instance object and set
that to title.
So now if we went in here right before we get stats we do person one set title we do SEO and then right
before person to person to set title
see I.
Now we should see that change so we can see CTO and CIO.
So we've set the title in the instance object completely separate and they continue to go throughout
the application.
Now this can be very powerful this is of some very very simple instance.
So another example might be port scan P Y so maybe you have this main class that calls or this main
source file that opens up and calls and creates a port scan copy of the class that handles all the port
scans.
So it handles one port scan for one host and you create another one for another host and another one
for another host.
So now you've gotten three port scanners running on their own without having to deal with writing all
of that code over and over and over and over again because everything's completely separate.