1
00:00:00,570 --> 00:00:04,920
In addition to creating attributes, we know that we can also create methods.

2
00:00:05,370 --> 00:00:09,360
The attributes are the things that the object has and the methods

3
00:00:09,420 --> 00:00:11,520
are the things that the object does.

4
00:00:12,330 --> 00:00:16,620
Let's say that we were creating the car class and we've really created the seats

5
00:00:16,650 --> 00:00:18,810
attribute. Well, later on

6
00:00:18,840 --> 00:00:23,840
we might decide that we want to call a method to change that seats attribute's

7
00:00:24,420 --> 00:00:29,370
value. So for example, I've been watching a lot of truck racing recently.

8
00:00:29,520 --> 00:00:29,720
Yeah.

9
00:00:29,720 --> 00:00:34,720
[inaudible]

10
00:00:35,690 --> 00:00:39,920
And one of the things that people do when they mod their cars for racing is they

11
00:00:39,920 --> 00:00:43,940
take out some of the seats. This way it reduces the weight of the car,

12
00:00:43,970 --> 00:00:48,170
and it means that you can go a bit faster. So in our car class,

13
00:00:48,200 --> 00:00:49,250
we might, in fact,

14
00:00:49,250 --> 00:00:53,780
have a method where the car can enter race mode,

15
00:00:54,170 --> 00:00:57,110
where the seats are dropped from 5 to 2.

16
00:00:58,010 --> 00:01:02,270
This is what the method would look like. Inside the class declaration,

17
00:01:02,630 --> 00:01:07,250
we have a function, but remember when a function is attached to an object

18
00:01:07,310 --> 00:01:08,810
then it's called a method.

19
00:01:09,830 --> 00:01:14,540
This method looks exactly the same as how we've created other functions.

20
00:01:14,540 --> 00:01:17,660
We have the Def keyword, we have the name of the method,

21
00:01:17,900 --> 00:01:21,470
we have some parentheses which can take inputs, a colon,

22
00:01:21,560 --> 00:01:24,650
and then the body of the method. So in this case,

23
00:01:24,650 --> 00:01:29,650
all it does is it gets hold of the object and then the object seats attribute

24
00:01:32,030 --> 00:01:36,620
and then changes it to 2. So now when you call that method,

25
00:01:36,710 --> 00:01:41,710
all you need to do is get hold of the object and then you use the dot notation

26
00:01:42,560 --> 00:01:46,310
to call that method enter_race_mode, and of course,

27
00:01:46,310 --> 00:01:47,750
with the parentheses at the end.

28
00:01:48,920 --> 00:01:52,640
So let's say that while we're modeling our Instagram user,

29
00:01:53,120 --> 00:01:56,720
we want to have a way for the users to follow each other, right?

30
00:01:57,140 --> 00:02:01,550
And when they follow each other, their follower counts obviously go up.

31
00:02:02,000 --> 00:02:06,890
So let's define a new method. So we're going to use the def keyword

32
00:02:07,190 --> 00:02:11,690
and then we're going to name our method follow. Now a method,

33
00:02:11,780 --> 00:02:16,780
unlike a function, always needs to have a self parameter as the first parameter.

34
00:02:18,980 --> 00:02:21,230
This means that when this method is called,

35
00:02:21,560 --> 00:02:25,490
it knows the object that called it. Now,

36
00:02:25,520 --> 00:02:27,380
in addition to that self parameter,

37
00:02:27,410 --> 00:02:31,430
we're also going to pass in the user that we've decided to follow.

38
00:02:32,270 --> 00:02:35,510
So now let's say that we actually had two attributes,

39
00:02:35,930 --> 00:02:39,860
the followers and the following count, right?

40
00:02:39,860 --> 00:02:43,430
So they both start from zero, so two default attributes.

41
00:02:44,000 --> 00:02:47,990
But when a user decides to follow another user,

42
00:02:48,500 --> 00:02:49,460
well in this case,

43
00:02:49,580 --> 00:02:54,580
the user who we're following, their follower count goes up by one and our own,

44
00:02:57,500 --> 00:03:02,500
so the self.following count goes up by one as well.

45
00:03:03,550 --> 00:03:07,930
So the self keyword becomes quite important when we're working with classes and

46
00:03:07,930 --> 00:03:12,760
objects. It's a way for us to refer to the object 

47
00:03:12,790 --> 00:03:17,790
that's going to be created from this class inside the class blueprint.

48
00:03:18,790 --> 00:03:22,030
So you'll never see self when you're using objects

49
00:03:22,540 --> 00:03:26,530
but you see it a lot when you're writing your code inside your class.

50
00:03:27,340 --> 00:03:30,070
Now let's go ahead and call this.

51
00:03:30,310 --> 00:03:35,310
And let's say that user_1 decided to follow

52
00:03:37,120 --> 00:03:41,530
user_2. This is the user_1 object,

53
00:03:41,980 --> 00:03:46,300
and this is the follow method from the user_1 object.

54
00:03:46,750 --> 00:03:50,560
And then the user_2 is the person who we're going to follow.

55
00:03:51,160 --> 00:03:55,630
So now let's go ahead and print the user_1's

56
00:03:56,050 --> 00:04:01,050
follower count and the user_1's following count.

57
00:04:03,280 --> 00:04:06,820
And let's do the same for user_2 as well.

58
00:04:09,370 --> 00:04:12,220
You can see once this method has run,

59
00:04:12,790 --> 00:04:16,089
then user_1's follow account is still zero,

60
00:04:16,390 --> 00:04:18,790
but user_1 is now following one person.

61
00:04:19,360 --> 00:04:24,340
User_2 has one followers and has zero people that it's following.

62
00:04:26,640 --> 00:04:26,730
Right?

