WEBVTT

0
00:00.570 --> 00:04.920
In addition to creating attributes, we know that we can also create methods.

1
00:05.370 --> 00:09.360
The attributes are the things that the object has and the methods

2
00:09.420 --> 00:11.520
are the things that the object does.

3
00:12.330 --> 00:16.620
Let's say that we were creating the car class and we've really created the seats

4
00:16.650 --> 00:18.810
attribute. Well, later on

5
00:18.840 --> 00:23.840
we might decide that we want to call a method to change that seats attribute's

6
00:24.420 --> 00:29.370
value. So for example, I've been watching a lot of truck racing recently.

7
00:29.520 --> 00:29.720
Yeah.

8
00:29.720 --> 00:34.720
<v 1>[inaudible]</v>

9
00:35.690 --> 00:39.920
<v 0>And one of the things that people do when they mod their cars for racing is they</v>

10
00:39.920 --> 00:43.940
take out some of the seats. This way it reduces the weight of the car,

11
00:43.970 --> 00:48.170
and it means that you can go a bit faster. So in our car class,

12
00:48.200 --> 00:49.250
we might, in fact,

13
00:49.250 --> 00:53.780
have a method where the car can enter race mode,

14
00:54.170 --> 00:57.110
where the seats are dropped from 5 to 2.

15
00:58.010 --> 01:02.270
This is what the method would look like. Inside the class declaration,

16
01:02.630 --> 01:07.250
we have a function, but remember when a function is attached to an object

17
01:07.310 --> 01:08.810
then it's called a method.

18
01:09.830 --> 01:14.540
This method looks exactly the same as how we've created other functions.

19
01:14.540 --> 01:17.660
We have the Def keyword, we have the name of the method,

20
01:17.900 --> 01:21.470
we have some parentheses which can take inputs, a colon,

21
01:21.560 --> 01:24.650
and then the body of the method. So in this case,

22
01:24.650 --> 01:29.650
all it does is it gets hold of the object and then the object seats attribute

23
01:32.030 --> 01:36.620
and then changes it to 2. So now when you call that method,

24
01:36.710 --> 01:41.710
all you need to do is get hold of the object and then you use the dot notation

25
01:42.560 --> 01:46.310
to call that method enter_race_mode, and of course,

26
01:46.310 --> 01:47.750
with the parentheses at the end.

27
01:48.920 --> 01:52.640
So let's say that while we're modeling our Instagram user,

28
01:53.120 --> 01:56.720
we want to have a way for the users to follow each other, right?

29
01:57.140 --> 02:01.550
And when they follow each other, their follower counts obviously go up.

30
02:02.000 --> 02:06.890
So let's define a new method. So we're going to use the def keyword

31
02:07.190 --> 02:11.690
and then we're going to name our method follow. Now a method,

32
02:11.780 --> 02:16.780
unlike a function, always needs to have a self parameter as the first parameter.

33
02:18.980 --> 02:21.230
This means that when this method is called,

34
02:21.560 --> 02:25.490
it knows the object that called it. Now,

35
02:25.520 --> 02:27.380
in addition to that self parameter,

36
02:27.410 --> 02:31.430
we're also going to pass in the user that we've decided to follow.

37
02:32.270 --> 02:35.510
So now let's say that we actually had two attributes,

38
02:35.930 --> 02:39.860
the followers and the following count, right?

39
02:39.860 --> 02:43.430
So they both start from zero, so two default attributes.

40
02:44.000 --> 02:47.990
But when a user decides to follow another user,

41
02:48.500 --> 02:49.460
well in this case,

42
02:49.580 --> 02:54.580
the user who we're following, their follower count goes up by one and our own,

43
02:57.500 --> 03:02.500
so the self.following count goes up by one as well.

44
03:03.550 --> 03:07.930
So the self keyword becomes quite important when we're working with classes and

45
03:07.930 --> 03:12.760
objects. It's a way for us to refer to the object 

46
03:12.790 --> 03:17.790
that's going to be created from this class inside the class blueprint.

47
03:18.790 --> 03:22.030
So you'll never see self when you're using objects

48
03:22.540 --> 03:26.530
but you see it a lot when you're writing your code inside your class.

49
03:27.340 --> 03:30.070
Now let's go ahead and call this.

50
03:30.310 --> 03:35.310
And let's say that user_1 decided to follow

51
03:37.120 --> 03:41.530
user_2. This is the user_1 object,

52
03:41.980 --> 03:46.300
and this is the follow method from the user_1 object.

53
03:46.750 --> 03:50.560
And then the user_2 is the person who we're going to follow.

54
03:51.160 --> 03:55.630
So now let's go ahead and print the user_1's

55
03:56.050 --> 04:01.050
follower count and the user_1's following count.

56
04:03.280 --> 04:06.820
And let's do the same for user_2 as well.

57
04:09.370 --> 04:12.220
You can see once this method has run,

58
04:12.790 --> 04:16.090
then user_1's follow account is still zero,

59
04:16.390 --> 04:18.790
but user_1 is now following one person.

60
04:19.360 --> 04:24.340
User_2 has one followers and has zero people that it's following.

61
04:26.640 --> 04:26.730
<v 2>Right?</v>