WEBVTT

0
00:00.060 --> 00:03.480
Now that we've seen how we can create a new class.

1
00:03.540 --> 00:06.060
Even if it's a class with nothing inside.

2
00:06.600 --> 00:10.290
And we've seen how we can create a new object from that class

3
00:10.740 --> 00:14.010
by using the same code that we used in yesterday's lessons.

4
00:14.430 --> 00:19.350
The next question is: "Well, how do we create an attribute for that class?". Well,

5
00:19.350 --> 00:24.350
one of the easiest ways of doing this is simply tapping into your object and

6
00:24.390 --> 00:26.130
then adding an attribute.

7
00:26.310 --> 00:31.310
So we could say user_1.id = 

8
00:32.460 --> 00:33.293
"001".

9
00:33.960 --> 00:38.960
And we could add as many attributes as we want just by using this method of

10
00:39.570 --> 00:42.570
using the dot notation after the name of the object,

11
00:42.960 --> 00:47.370
and then just adding any piece of data we want. So now,

12
00:47.370 --> 00:51.930
if I go ahead and print user_1.username,

13
00:52.290 --> 00:55.860
you'll see that this attribute now exists in this object

14
00:55.880 --> 00:56.713
[Inaudible]

15
00:58.910 --> 01:03.470
<v 0>And you can see the value that's held in that attribute being printed in the</v>

16
01:03.470 --> 01:05.870
console. So just a quick reminder,

17
01:05.900 --> 01:10.820
an attribute is a variable that's associated with an object.

18
01:11.450 --> 01:16.130
So it's almost as if we just created a new variable, but in this case,

19
01:16.160 --> 01:20.630
we've attached it to an object. But here's a question though,

20
01:21.710 --> 01:25.640
if we had lots and lots of attributes like this,

21
01:25.880 --> 01:29.030
and every single time we create a new user,

22
01:29.420 --> 01:34.420
let's say we created user_2 and we had to do the same thing,

23
01:34.550 --> 01:36.830
create it from the class and then say,

24
01:36.830 --> 01:41.000
user_2.id = "002",

25
01:41.120 --> 01:43.910
and user_2.username

26
01:45.440 --> 01:49.430
= "jack". And this is one,

27
01:49.610 --> 01:54.500
a little bit prone to error because it's perfectly valid for me to make a typo

28
01:54.530 --> 01:58.940
or change the name of an attribute where I end up with something like this.

29
01:59.300 --> 02:02.120
So is there a way of making this simpler?

30
02:02.510 --> 02:06.770
How can I specify all these starting pieces of information

31
02:07.040 --> 02:11.300
when I create my object from the class? Now,

32
02:11.330 --> 02:15.320
in order to do this, we have to understand something called a constructor,

33
02:15.770 --> 02:20.770
which is a part of the blueprint that allows us to specify what should happen

34
02:21.470 --> 02:24.410
when our object is being constructed.

35
02:25.160 --> 02:29.240
And this is also known in programming as initializing

36
02:29.270 --> 02:32.840
an object. When the object is being initialized,

37
02:32.870 --> 02:37.870
we can set variables or counters to they're starting values. In Python,

38
02:39.560 --> 02:44.560
the way that we would create the constructor is by using a special function,

39
02:45.980 --> 02:47.570
which is the init function.

40
02:48.140 --> 02:52.070
And you can tell it's special because it isn't just the Def keyword

41
02:52.310 --> 02:55.850
and then the name of the function. It's got two underscores

42
02:55.910 --> 02:57.110
either side of the name.

43
02:57.740 --> 03:02.740
And this means that it's a method that the Python interpreter knows about and

44
03:03.100 --> 03:05.800
knows that it has a special function. Now,

45
03:05.800 --> 03:10.800
what is the special function? Well, it's normally used to initialize the attributes.

46
03:12.580 --> 03:14.260
So if inside our class

47
03:14.590 --> 03:19.590
we add this init function and you can see as soon as I write def and then 

48
03:19.690 --> 03:23.320
init, I've really got autofill helping me so I can just hit enter

49
03:23.620 --> 03:25.510
once it's selected. Now,

50
03:25.630 --> 03:30.630
inside this init function is where we initialize or create the starting

51
03:30.970 --> 03:33.940
values for our attributes. Now,

52
03:34.000 --> 03:39.000
the important thing to remember is that the init function is going to be called

53
03:39.370 --> 03:43.210
every time you create a new object from this class.

54
03:43.690 --> 03:45.400
For example, if I print,

55
03:47.830 --> 03:51.460
then every time that this construction happens

56
03:51.730 --> 03:53.770
when we create a new user,

57
03:54.130 --> 03:57.220
then this print statement is going to be triggered.

58
03:59.920 --> 04:03.940
So you can see here, this print statement gets triggered

59
04:04.360 --> 04:08.410
and then we print the user_1's username, and then we've got 

60
04:08.410 --> 04:12.490
another user being created. Just as a quick reminder,

61
04:12.520 --> 04:16.720
the attributes are the things that the object will have.

62
04:17.350 --> 04:22.210
And they are basically just variables that are associated with the final

63
04:22.210 --> 04:25.870
object. So for example, if you're constructing a car bject,

64
04:25.900 --> 04:29.500
then one of those attributes could be the number of seats it has and

65
04:29.500 --> 04:31.150
normally that's equal to five.

66
04:31.720 --> 04:36.220
So how do we set these attributes in the constructor? Well,

67
04:36.220 --> 04:37.930
this is what the code would look like.

68
04:38.350 --> 04:42.460
We have our init function and inside the init function

69
04:42.820 --> 04:44.950
in addition to this thing called self

70
04:45.010 --> 04:50.010
which is the actual object that's being created or being initialized,

71
04:50.890 --> 04:54.250
in addition, you can add as many parameters as you wish.

72
04:54.670 --> 04:59.670
And that parameter is going to be passed in when an object gets constructed from

73
05:00.850 --> 05:04.300
this class. And once you receive that data,

74
05:04.390 --> 05:09.010
then you can use it to set the object's attributes.

75
05:10.090 --> 05:14.470
This is what it would look like when we actually call our constructor.

76
05:15.250 --> 05:20.250
We know that we can create an object by calling the name of the class and then

77
05:20.980 --> 05:25.810
adding the parentheses. But if inside our init function

78
05:25.900 --> 05:27.970
we have some additional parameters,

79
05:28.240 --> 05:32.350
then we can also pass in data to those parameters

80
05:32.470 --> 05:36.460
which will be used to set the attributes for that object.

81
05:36.760 --> 05:40.330
So in this case, once this line of code has completed,

82
05:40.510 --> 05:44.710
then my_car.seats is going to be equal to five.

83
05:45.790 --> 05:49.390
And this is exactly the same as if we created our

84
05:49.390 --> 05:53.560
my_car object first and then we said my_car.seats = 5.

85
05:53.590 --> 05:55.180
It's completely the same.

86
05:56.230 --> 06:00.500
But it does make it a lot quicker when you're creating a lot of objects that

87
06:00.500 --> 06:05.150
need all the same attributes. Let's see how we would do it here.

88
06:05.150 --> 06:07.400
We've got our init function already declared.

89
06:08.000 --> 06:11.780
So if we wanted to pass over a user ID

90
06:11.810 --> 06:15.890
when we construct our objects, we can add that as a parameter

91
06:16.340 --> 06:20.060
and then inside the init we can say self.id,

92
06:20.330 --> 06:25.330
so self.id is going to be the attribute that's associated with this class

93
06:25.910 --> 06:30.910
and we can set it equal to the user_id that's passed in when any user gets

94
06:31.580 --> 06:35.450
constructed. So now when we create our user,

95
06:36.050 --> 06:40.880
instead of creating these attributes later on, at the time of construction

96
06:41.000 --> 06:44.420
you can already see that we can add a user ID.

97
06:45.170 --> 06:47.000
So let's give it the same ID.

98
06:48.020 --> 06:50.990
And now let's add another attribute.

99
06:51.320 --> 06:56.320
So we're going to pass in the username and I'm going to set the attribute

100
06:56.930 --> 07:00.710
self.username equal the username that's past it.

101
07:01.280 --> 07:05.510
Notice how in this case I've named my parameter user_id

102
07:05.840 --> 07:10.640
and that value is going to become the self.id attribute.

103
07:11.060 --> 07:14.120
So notice how these two actually have different names.

104
07:14.510 --> 07:17.480
You can name either of these anything you want.

105
07:18.230 --> 07:23.120
It's normally convention that you will see the name of the parameter be equal to

106
07:23.120 --> 07:27.590
the name of the attribute, but you don't always have to stick to these rules.

107
07:28.970 --> 07:33.590
Now let's go back to our constructor whereas previously it was empty,

108
07:33.830 --> 07:38.830
now we have our user ID being passed in and I can now pass in the second

109
07:39.620 --> 07:44.060
attribute, which is the username. So my username was going to be Angela.

110
07:45.650 --> 07:49.370
And now we've created a new user,

111
07:49.520 --> 07:53.480
which is user_1 from this user class

112
07:53.690 --> 07:58.690
and we're initializing it with these starting values where the first value is

113
07:59.690 --> 08:03.860
going to become the user ID which is going to be the self.id,

114
08:04.220 --> 08:08.780
and the second value is going to become the self.username. So now,

115
08:08.780 --> 08:12.560
if I wanted to print the user_1's username,

116
08:12.950 --> 08:14.420
then I can simply write

117
08:14.420 --> 08:17.690
user_1.username exactly the same way as before,

118
08:17.990 --> 08:21.350
or if I wanted the ID, it would be user_1.id.

119
08:22.070 --> 08:26.750
So now let's comment out the rest of the code and hit run,

120
08:27.680 --> 08:32.180
and you can see we've got the user ID being printed here. Now,

121
08:32.180 --> 08:35.540
remember that when you add parameters to the constructor

122
08:35.660 --> 08:36.920
which is the init function,

123
08:37.460 --> 08:42.460
you're now saying that whenever a new object is being constructed from this

124
08:43.130 --> 08:47.600
class, it must provide these two pieces of data.

125
08:48.530 --> 08:53.150
If we uncomment this and you try to run it, you'll see you get an error.

126
08:53.840 --> 08:56.160
And it says that on line 12,

127
08:56.940 --> 09:00.840
this user is being created from the user class.

128
09:01.320 --> 09:06.180
But according to the init, we're actually missing two required arguments;

129
09:06.360 --> 09:08.010
user ID and username.

130
09:08.790 --> 09:12.300
So now when we create any object from this class,

131
09:12.600 --> 09:16.290
we have to provide the user ID and the username.

132
09:17.190 --> 09:20.190
But this is now much more convenient, right?

133
09:20.250 --> 09:23.970
While we previously needed three lines of code, we now only have one.

134
09:25.200 --> 09:27.540
Now sometimes when we're creating our attributes,

135
09:27.840 --> 09:31.170
we might want a default value to start with.

136
09:31.230 --> 09:35.460
And if at some later point in our program we want to modify it,

137
09:35.460 --> 09:36.840
then we'll do it at that point.

138
09:37.140 --> 09:42.140
But it doesn't make sense for all attributes to be initialized when we actually

139
09:42.510 --> 09:46.200
create our object. So if, for example, in our case,

140
09:46.230 --> 09:50.010
let's say that we were coding up the Instagram app, right? Well,

141
09:50.010 --> 09:53.160
each of the Instagram users, they would have an ID,

142
09:53.430 --> 09:56.940
they would have a username and they would probably have a follower count.

143
09:57.330 --> 10:01.170
So they might have something like self.followers.

144
10:02.460 --> 10:07.050
This is always going to start out as zero, right? You start a new account,

145
10:07.110 --> 10:10.350
you're going to have zero followers. You might get some followers later on,

146
10:10.620 --> 10:11.453
you might not.

147
10:11.700 --> 10:16.700
But it doesn't make sense for followers to be something that we have to provide

148
10:17.520 --> 10:19.410
whenever we construct a new object.

149
10:19.680 --> 10:23.670
It seems very wasteful to have to always write zero

150
10:23.880 --> 10:27.900
when you construct any object from this class. So in Python,

151
10:27.900 --> 10:31.770
we can also provide a default value just as I've done here.

152
10:32.220 --> 10:37.170
Instead of setting it equal to one of the parameters that's being passed in when

153
10:37.170 --> 10:41.610
this class is being initialized, instead I'm just setting it to a value,

154
10:41.640 --> 10:46.470
which is zero. Let's get rid of that from the initializer

155
10:46.500 --> 10:48.840
because we now have a default value of zero.

156
10:49.380 --> 10:52.530
And if I go ahead and print my, let's say,

157
10:52.530 --> 10:57.180
user_1.followers attribute, and I hit run,

158
10:57.570 --> 11:02.570
then you can see it's zero because all objects being created from this class will

159
11:04.170 --> 11:07.830
have a followers attribute that set out to zero to begin with.