In addition to creating attributes, we know that we can also create methods.
The attributes are the things that the object has and the methods
are the things that the object does.
Let's say that we were creating the car class and we've really created the seats
attribute. Well, later on
we might decide that we want to call a method to change that seats attribute's
value. So for example, I've been watching a lot of truck racing recently.
Yeah.
[inaudible]
And one of the things that people do when they mod their cars for racing is they
take out some of the seats. This way it reduces the weight of the car,
and it means that you can go a bit faster. So in our car class,
we might, in fact,
have a method where the car can enter race mode,
where the seats are dropped from 5 to 2.
This is what the method would look like. Inside the class declaration,
we have a function, but remember when a function is attached to an object
then it's called a method.
This method looks exactly the same as how we've created other functions.
We have the Def keyword, we have the name of the method,
we have some parentheses which can take inputs, a colon,
and then the body of the method. So in this case,
all it does is it gets hold of the object and then the object seats attribute
and then changes it to 2. So now when you call that method,
all you need to do is get hold of the object and then you use the dot notation
to call that method enter_race_mode, and of course,
with the parentheses at the end.
So let's say that while we're modeling our Instagram user,
we want to have a way for the users to follow each other, right?
And when they follow each other, their follower counts obviously go up.
So let's define a new method. So we're going to use the def keyword
and then we're going to name our method follow. Now a method,
unlike a function, always needs to have a self parameter as the first parameter.
This means that when this method is called,
it knows the object that called it. Now,
in addition to that self parameter,
we're also going to pass in the user that we've decided to follow.
So now let's say that we actually had two attributes,
the followers and the following count, right?
So they both start from zero, so two default attributes.
But when a user decides to follow another user,
well in this case,
the user who we're following, their follower count goes up by one and our own,
so the self.following count goes up by one as well.
So the self keyword becomes quite important when we're working with classes and
objects. It's a way for us to refer to the object
that's going to be created from this class inside the class blueprint.
So you'll never see self when you're using objects
but you see it a lot when you're writing your code inside your class.
Now let's go ahead and call this.
And let's say that user_1 decided to follow
user_2. This is the user_1 object,
and this is the follow method from the user_1 object.
And then the user_2 is the person who we're going to follow.
So now let's go ahead and print the user_1's
follower count and the user_1's following count.
And let's do the same for user_2 as well.
You can see once this method has run,
then user_1's follow account is still zero,
but user_1 is now following one person.
User_2 has one followers and has zero people that it's following.
Right?