1
1

00:00:00,130  -->  00:00:01,970
<v Instructor>Hi guys and welcome back.</v>
2

2

00:00:01,970  -->  00:00:03,890
In this video we're gonna talk about
3

3

00:00:03,890  -->  00:00:06,140
object oriented programming.
4

4

00:00:06,140  -->  00:00:08,790
Object oriented programming is a bit confusing
5

5

00:00:08,790  -->  00:00:11,070
when you get started, so if you've never seen
6

6

00:00:11,070  -->  00:00:13,950
object oriented programming before, just take it easy
7

7

00:00:13,950  -->  00:00:16,410
with the next few videos, watch them a couple of times
8

8

00:00:16,410  -->  00:00:18,330
if you need to, ask any questions in the course
9

9

00:00:18,330  -->  00:00:22,000
of the Q and A, if necessary we'll be there to help you out.
10

10

00:00:22,000  -->  00:00:24,780
Object oriented programming allows us as developers
11

11

00:00:24,780  -->  00:00:28,260
to write code in a slightly different way
12

12

00:00:28,260  -->  00:00:30,250
than we would write if we didn't have
13

13

00:00:30,250  -->  00:00:32,140
object oriented programming.
14

14

00:00:32,140  -->  00:00:34,900
The purpose of object oriented programming
15

15

00:00:34,900  -->  00:00:37,274
is to make our life as developers simpler
16

16

00:00:37,274  -->  00:00:41,641
by allowing us to write code that looks like
17

17

00:00:41,641  -->  00:00:44,480
what we would work with in the real world
18

18

00:00:44,480  -->  00:00:46,750
if we were talking about, you know,
19

19

00:00:46,750  -->  00:00:49,110
real world things like chairs and tables
20

20

00:00:49,110  -->  00:00:50,640
and printers and things like that,
21

21

00:00:50,640  -->  00:00:53,860
as opposed to code and data passing
22

22

00:00:53,860  -->  00:00:55,860
from one function to another.
23

23

00:00:55,860  -->  00:00:58,250
So that is the purpose of object oriented programming.
24

24

00:00:58,250  -->  00:00:59,990
And in this video we're going to look at how
25

25

00:00:59,990  -->  00:01:01,163
it works in Python.
26

26

00:01:02,260  -->  00:01:05,820
Let's say that you've got a dictionary like this one here
27

27

00:01:05,820  -->  00:01:09,370
which shows some information about a student.
28

28

00:01:09,370  -->  00:01:12,920
Now clearly this thing here is not a student,
29

29

00:01:12,920  -->  00:01:15,160
it is a dictionary that has some data.
30

30

00:01:15,160  -->  00:01:18,160
And as we know, programming is all about data,
31

31

00:01:18,160  -->  00:01:20,860
passing data from one place to another and so forth.
32

32

00:01:20,860  -->  00:01:23,500
So what we've got here is some data that is probably
33

33

00:01:23,500  -->  00:01:25,810
about a student somewhere in our classroom
34

34

00:01:25,810  -->  00:01:27,113
or something like that.
35

35

00:01:27,970  -->  00:01:31,012
Then we've got a function that takes in a sequence
36

36

00:01:31,012  -->  00:01:35,130
and adds the entire sequence together and divides it
37

37

00:01:35,130  -->  00:01:37,690
by the length of the sequence and then returns that.
38

38

00:01:37,690  -->  00:01:40,630
So it gives you the average of a sequence.
39

39

00:01:40,630  -->  00:01:43,120
For example we could use it to calculate the average
40

40

00:01:43,120  -->  00:01:45,180
grade of Rolf.
41

41

00:01:45,180  -->  00:01:46,520
How would you do that?
42

42

00:01:46,520  -->  00:01:48,739
Well you would have to use the function
43

43

00:01:48,739  -->  00:01:52,160
and give to it Rolf's grades.
44

44

00:01:52,160  -->  00:01:57,160
So you do average student grades, like that.
45

45

00:01:58,380  -->  00:02:01,540
So if we run that you'll see that we get out
46

46

00:02:01,540  -->  00:02:05,760
the value 88.0 which is the average grade.
47

47

00:02:05,760  -->  00:02:09,230
However, there is something about this function
48

48

00:02:09,230  -->  00:02:13,190
which is very important when you are thinking
49

49

00:02:13,190  -->  00:02:14,490
about object oriented programming,
50

50

00:02:14,490  -->  00:02:17,236
which is that this function doesn't know anything
51

51

00:02:17,236  -->  00:02:20,761
about the student.
52

52

00:02:20,761  -->  00:02:25,050
The function only operates on the input that it receives
53

53

00:02:25,050  -->  00:02:27,510
and that could be a set of student grades
54

54

00:02:27,510  -->  00:02:29,393
or it could be something else.
55

55

00:02:30,713  -->  00:02:34,850
So when we as developers are talking specifically
56

56

00:02:34,850  -->  00:02:37,460
about a student and we want to calculate specifically
57

57

00:02:37,460  -->  00:02:42,460
the average student grade for Rolf, it is much nicer
58

58

00:02:42,690  -->  00:02:46,435
if we can say hey student, what's your average
59

59

00:02:46,435  -->  00:02:51,170
as opposed to get the average of this sequence
60

60

00:02:51,170  -->  00:02:54,490
that could or could not be related to the student.
61

61

00:02:54,490  -->  00:02:59,462
So basically what I want to do is student.average.
62

62

00:02:59,462  -->  00:03:02,240
This is what object oriented programming
63

63

00:03:02,240  -->  00:03:04,430
will allow us to do.
64

64

00:03:04,430  -->  00:03:09,130
But in order to do that, student cannot be a dictionary
65

65

00:03:09,130  -->  00:03:12,180
because dictionaries you can't do that on.
66

66

00:03:12,180  -->  00:03:16,330
You have to code your own thing in Python that will
67

67

00:03:16,330  -->  00:03:21,180
allow you to call the average method inside it,
68

68

00:03:21,180  -->  00:03:24,390
and that will give you the average of the student.
69

69

00:03:24,390  -->  00:03:26,820
So it is a very different approach,
70

70

00:03:26,820  -->  00:03:30,760
and it is not necessarily superior,
71

71

00:03:30,760  -->  00:03:32,280
but it is different and allows
72

72

00:03:32,280  -->  00:03:35,000
you to think of your programmes in a different way.
73

73

00:03:35,000  -->  00:03:37,920
So let's rewrite this example here using object
74

74

00:03:37,920  -->  00:03:41,830
oriented programming so you'll have a look at the syntax.
75

75

00:03:41,830  -->  00:03:43,470
The syntax for object oriented programming
76

76

00:03:43,470  -->  00:03:46,900
or to define a class in Python is to type out a keyword
77

77

00:03:46,900  -->  00:03:50,160
class, and then the name of your class.
78

78

00:03:50,160  -->  00:03:53,560
So a class is a definition of something,
79

79

00:03:53,560  -->  00:03:56,680
but it doesn't create a particular student here,
80

80

00:03:56,680  -->  00:03:58,980
we're only defining how a student will behave.
81

81

00:04:00,080  -->  00:04:03,870
So you can say inside the function,
82

82

00:04:03,870  -->  00:04:06,680
notice that there are four spaces of indentation,
83

83

00:04:06,680  -->  00:04:08,450
you can define functions.
84

84

00:04:08,450  -->  00:04:10,760
So you can define a special function called
85

85

00:04:10,760  -->  00:04:15,090
the underscore underscore init underscore underscore.
86

86

00:04:15,090  -->  00:04:17,500
So this function name is very important,
87

87

00:04:17,500  -->  00:04:19,010
and it has two underscores at the start
88

88

00:04:19,010  -->  00:04:20,520
and two at the end.
89

89

00:04:20,520  -->  00:04:23,880
And then you put your brackets and in here we're going
90

90

00:04:23,880  -->  00:04:26,040
to type out self.
91

91

00:04:26,040  -->  00:04:28,610
Although this is again just a variable name
92

92

00:04:28,610  -->  00:04:30,470
like in every other function and you can call it
93

93

00:04:30,470  -->  00:04:31,770
whatever you want, it doesn't have to be self
94

94

00:04:31,770  -->  00:04:35,023
if you don't want, but self is the convention in Python.
95

95

00:04:36,060  -->  00:04:39,170
Then in the body of the function you can define
96

96

00:04:39,170  -->  00:04:40,520
anything you want.
97

97

00:04:40,520  -->  00:04:44,820
For example, self.name is going to be Rolf.
98

98

00:04:44,820  -->  00:04:48,230
And what this is doing is it is taking whatever self is
99

99

00:04:50,070  -->  00:04:54,390
and it is accessing the name property inside of self.
100

100

00:04:54,390  -->  00:04:57,130
So here we're using the dot notation
101

101

00:04:57,130  -->  00:04:59,420
to access something inside of self,
102

102

00:04:59,420  -->  00:05:02,060
and what we're accessing is the name property.
103

103

00:05:02,060  -->  00:05:04,970
And then we're giving it a value of Rolf.
104

104

00:05:04,970  -->  00:05:09,600
What that means is that when we use this class
105

105

00:05:09,600  -->  00:05:12,950
to create something, that thing is going
106

106

00:05:12,950  -->  00:05:14,180
to have a name of Rolf.
107

107

00:05:14,180  -->  00:05:17,310
So here's how you create a thing from a class.
108

108

00:05:17,310  -->  00:05:20,297
We can say something like student, notice that now
109

109

00:05:20,297  -->  00:05:21,580
this is lowercase because this is a new variable
110

110

00:05:21,580  -->  00:05:23,400
that we're creating right now,
111

111

00:05:23,400  -->  00:05:28,400
is a student, which is uppercase S because this the class.
112

112

00:05:28,900  -->  00:05:31,560
And now we're going to tell it to use the class
113

113

00:05:31,560  -->  00:05:35,160
to create what's called an object in Python
114

114

00:05:35,160  -->  00:05:38,500
to create a new thing that behaves
115

115

00:05:38,500  -->  00:05:40,930
like what this class defines.
116

116

00:05:40,930  -->  00:05:44,380
So when you do this Python creates a new
117

117

00:05:44,380  -->  00:05:47,460
essentially empty container if you will
118

118

00:05:47,460  -->  00:05:50,070
and it runs the init method.
119

119

00:05:50,070  -->  00:05:52,260
By the way it is a method and not a function
120

120

00:05:52,260  -->  00:05:54,270
because it is inside of a class.
121

121

00:05:54,270  -->  00:05:55,810
So when a function is inside of a class
122

122

00:05:55,810  -->  00:05:57,460
it's called a method.
123

123

00:05:57,460  -->  00:06:00,420
So when you call the student class,
124

124

00:06:00,420  -->  00:06:02,030
notice that this bracket notation here
125

125

00:06:02,030  -->  00:06:03,950
is used to call a function normally.
126

126

00:06:03,950  -->  00:06:05,950
But we can also use it to call a class.
127

127

00:06:05,950  -->  00:06:08,430
When you call that Python will automatically
128

128

00:06:08,430  -->  00:06:12,610
call the init method for you and it will create
129

129

00:06:12,610  -->  00:06:16,720
this empty thing called self, and you will be able
130

130

00:06:16,720  -->  00:06:20,300
to modify the name property inside self
131

131

00:06:20,300  -->  00:06:22,080
and give it the value Rolf.
132

132

00:06:22,080  -->  00:06:24,550
The name property won't exist by default so doing
133

133

00:06:24,550  -->  00:06:26,990
this also creates it, which is quite nice.
134

134

00:06:26,990  -->  00:06:31,690
So once that's done and this self object contains
135

135

00:06:31,690  -->  00:06:36,690
a name property, Python will give you back that self.
136

136

00:06:37,930  -->  00:06:40,530
And student will become a name
137

137

00:06:40,530  -->  00:06:43,480
for the self thing we created,
138

138

00:06:43,480  -->  00:06:46,550
that thing being an object, that's what they're called.
139

139

00:06:46,550  -->  00:06:49,710
So we're using this class through the init method
140

140

00:06:49,710  -->  00:06:53,010
to create an object and assign the name property
141

141

00:06:53,010  -->  00:06:56,170
inside that object to the string Rolf.
142

142

00:06:56,170  -->  00:06:59,730
Then that object is what becomes the value
143

143

00:06:59,730  -->  00:07:01,920
for our student variable.
144

144

00:07:01,920  -->  00:07:05,590
So you can now do print student.name,
145

145

00:07:05,590  -->  00:07:07,320
and here what we're doing is we're accessing
146

146

00:07:07,320  -->  00:07:10,340
the name property of our student variable
147

147

00:07:10,340  -->  00:07:12,760
which as you know is the self
148

148

00:07:12,760  -->  00:07:17,580
that we created earlier on, so that will give you Rolf.
149

149

00:07:17,580  -->  00:07:21,940
And if I run that you see that Rolf comes out.
150

150

00:07:21,940  -->  00:07:25,940
This is how object oriented programming works in Python.
151

151

00:07:25,940  -->  00:07:29,220
You call a class, the init method runs,
152

152

00:07:29,220  -->  00:07:31,860
and what you get back is the object you created
153

153

00:07:31,860  -->  00:07:33,890
and in the init method you have the opportunity
154

154

00:07:33,890  -->  00:07:36,980
to set and change its properties.
155

155

00:07:36,980  -->  00:07:38,750
So what we've got here is the name property
156

156

00:07:38,750  -->  00:07:40,210
that we're changing.
157

157

00:07:40,210  -->  00:07:43,670
As well as self.name equals Rolf, we can do self.grades
158

158

00:07:43,670  -->  00:07:45,863
and make it equal to what we had before,
159

159

00:07:47,290  -->  00:07:48,123
something like that.
160

160

00:07:48,123  -->  00:07:51,270
So we're making the grades property of self
161

161

00:07:51,270  -->  00:07:52,670
equal to this tuple.
162

162

00:07:52,670  -->  00:07:54,720
And then when the function finishes,
163

163

00:07:54,720  -->  00:07:57,780
the class gives back self to student
164

164

00:07:57,780  -->  00:08:01,400
and now you can print out student.grades
165

165

00:08:01,400  -->  00:08:04,550
if you want and that property will exist there as well,
166

166

00:08:04,550  -->  00:08:05,383
as you can see.
167

167

00:08:06,800  -->  00:08:08,900
So nothing terribly complicated up til now.
168

168

00:08:08,900  -->  00:08:10,500
There's a bit of data being passed from
169

169

00:08:10,500  -->  00:08:11,760
one place to another,
170

170

00:08:11,760  -->  00:08:15,330
but this is no different to what we had before really.
171

171

00:08:15,330  -->  00:08:17,650
If you wanted to get the average of these grades
172

172

00:08:17,650  -->  00:08:21,150
you would still have to pass them to a function
173

173

00:08:21,150  -->  00:08:24,570
called average for example and that would give you
174

174

00:08:24,570  -->  00:08:26,840
the average if you had the function defined.
175

175

00:08:26,840  -->  00:08:28,890
But using object oriented programming,
176

176

00:08:28,890  -->  00:08:30,970
there's one more thing you can do which is going
177

177

00:08:30,970  -->  00:08:33,360
to simplify your life greatly, and that is the main
178

178

00:08:33,360  -->  00:08:35,047
point of object oriented programming,
179

179

00:08:35,047  -->  00:08:38,106
which is to define another method such as
180

180

00:08:38,106  -->  00:08:42,450
the average method inside the class.
181

181

00:08:42,450  -->  00:08:47,450
And this method will take self as a parameter.
182

182

00:08:48,515  -->  00:08:51,660
Notice that self again doesn't have to be self,
183

183

00:08:51,660  -->  00:08:53,140
you can call it whatever you want.
184

184

00:08:53,140  -->  00:08:55,360
That's just the convention in Python,
185

185

00:08:55,360  -->  00:08:56,830
we usually call it self but you can call it
186

186

00:08:56,830  -->  00:08:57,740
whatever you want.
187

187

00:08:57,740  -->  00:09:00,312
So if you define a method inside a class,
188

188

00:09:00,312  -->  00:09:04,680
all the methods inside the class have to take a parameter
189

189

00:09:04,680  -->  00:09:09,223
which will be the object that was created initially.
190

190

00:09:10,620  -->  00:09:14,810
You can do return sum of self.grades divided by len
191

191

00:09:14,810  -->  00:09:16,173
of self.grades.
192

192

00:09:17,090  -->  00:09:22,090
Notice how this method here does not take a sequence,
193

193

00:09:22,860  -->  00:09:27,170
it just uses the object that was created up here.
194

194

00:09:27,170  -->  00:09:31,758
So how does Python know that it needs to call
195

195

00:09:31,758  -->  00:09:35,200
this method with this data here.
196

196

00:09:35,200  -->  00:09:38,270
Well here's how it works internally.
197

197

00:09:38,270  -->  00:09:40,710
When you create a new class, the init method
198

198

00:09:40,710  -->  00:09:45,710
runs and you get this new object placed in student.
199

199

00:09:46,061  -->  00:09:51,061
Then whenever you want to call this method here,
200

200

00:09:51,270  -->  00:09:56,158
all you have to do is type out student, the class,
201

201

00:09:56,158  -->  00:10:01,158
dot average, and then of course because this is essentially
202

202

00:10:01,320  -->  00:10:06,133
another function, you put in there the student object.
203

203

00:10:07,128  -->  00:10:12,128
And what this is doing is it's hauling the average method
204

204

00:10:12,140  -->  00:10:13,740
inside, that's what the dot means,
205

205

00:10:13,740  -->  00:10:15,380
inside the student class,
206

206

00:10:15,380  -->  00:10:16,940
so that's this one there.
207

207

00:10:16,940  -->  00:10:19,970
And the self parameter is going to take the value
208

208

00:10:19,970  -->  00:10:23,360
of the student variable which has that object
209

209

00:10:23,360  -->  00:10:25,210
that was created earlier on that contains
210

210

00:10:25,210  -->  00:10:27,060
the name and the grades.
211

211

00:10:27,060  -->  00:10:29,420
So if we save that and run it,
212

212

00:10:29,420  -->  00:10:31,563
you can see that you get 88.2.
213

213

00:10:32,850  -->  00:10:35,910
Now this is a little bit verbose, there's a few
214

214

00:10:35,910  -->  00:10:37,770
too many words in there, so Python has a nice bit
215

215

00:10:37,770  -->  00:10:40,002
of syntax that you can use instead of this one
216

216

00:10:40,002  -->  00:10:43,960
which is to remove the student argument
217

217

00:10:43,960  -->  00:10:46,400
from the average function and instead of calling
218

218

00:10:46,400  -->  00:10:49,500
the average method inside the class,
219

219

00:10:49,500  -->  00:10:52,481
call it on the object itself.
220

220

00:10:52,481  -->  00:10:56,370
So if you do student.average, Python is going
221

221

00:10:56,370  -->  00:11:01,020
to realise that student is a student object,
222

222

00:11:01,020  -->  00:11:03,240
so you probably want to pass it as self,
223

223

00:11:03,240  -->  00:11:05,090
as the first argument.
224

224

00:11:05,090  -->  00:11:07,360
So it will do the conversion for you,
225

225

00:11:07,360  -->  00:11:10,200
that is the same thing and one is the other,
226

226

00:11:10,200  -->  00:11:11,380
but this is a little bit shorter,
227

227

00:11:11,380  -->  00:11:12,680
a little bit easier to read.
228

228

00:11:12,680  -->  00:11:15,830
And if you name your variables well,
229

229

00:11:15,830  -->  00:11:18,287
then this is pretty obvious what it's gonna give you back,
230

230

00:11:18,287  -->  00:11:20,860
it's gonna give you the average of this student.
231

231

00:11:20,860  -->  00:11:24,680
Now you can make it even better, type average grade say,
232

232

00:11:24,680  -->  00:11:26,190
and that's going to be just a little bit
233

233

00:11:26,190  -->  00:11:27,580
more readable as well.
234

234

00:11:27,580  -->  00:11:29,510
And just to show you that this works,
235

235

00:11:29,510  -->  00:11:31,210
it does exactly the same thing.
236

236

00:11:31,210  -->  00:11:34,070
So this is the key benefit of object oriented programming.
237

237

00:11:34,070  -->  00:11:36,960
You can define methods inside a class
238

238

00:11:36,960  -->  00:11:39,880
that use the object that was initially created
239

239

00:11:39,880  -->  00:11:42,631
by the init method within them.
240

240

00:11:42,631  -->  00:11:43,960
Show that and you don't have to keep track
241

241

00:11:43,960  -->  00:11:45,890
of multiple different pieces of data
242

242

00:11:45,890  -->  00:11:50,000
and the object itself can contain these methods
243

243

00:11:50,000  -->  00:11:51,190
that you want to use inside it,
244

244

00:11:51,190  -->  00:11:55,640
so all the data and the methods are in one place
245

245

00:11:55,640  -->  00:11:57,078
inside the class.
246

246

00:11:57,078  -->  00:12:00,260
Now the init method can also take arguments
247

247

00:12:00,260  -->  00:12:01,700
or have parameters.
248

248

00:12:01,700  -->  00:12:06,340
For example, we can have a name parameter as well as self,
249

249

00:12:06,340  -->  00:12:09,900
so just to recap, all methods inside a class
250

250

00:12:09,900  -->  00:12:14,408
need to have a parameter such as self
251

251

00:12:14,408  -->  00:12:16,711
so they can take in the object that
252

252

00:12:16,711  -->  00:12:19,210
you have been constructing.
253

253

00:12:19,210  -->  00:12:22,330
But they can also take more arguments,
254

254

00:12:22,330  -->  00:12:24,630
so here we're going to have another parameter,
255

255

00:12:24,630  -->  00:12:27,347
the name parameter, and then you'll put the value
256

256

00:12:27,347  -->  00:12:30,000
of that parameter in the bracket
257

257

00:12:30,000  -->  00:12:32,480
of the student class call.
258

258

00:12:32,480  -->  00:12:36,163
So we can put here Bob, let's say.
259

259

00:12:36,163  -->  00:12:40,150
So now Bob will become the value for name.
260

260

00:12:40,150  -->  00:12:43,060
Notice that self is given for you, so you don't have
261

261

00:12:43,060  -->  00:12:45,420
to give it a name, this here will become
262

262

00:12:45,420  -->  00:12:46,633
the value for name.
263

263

00:12:47,760  -->  00:12:52,570
But notice that we're not using this name parameter anyway.
264

264

00:12:52,570  -->  00:12:56,010
We are accessing here self.name, but that's the name
265

265

00:12:56,010  -->  00:13:00,410
property inside self, it is not this parameter.
266

266

00:13:00,410  -->  00:13:02,020
If you wanted to access this parameter,
267

267

00:13:02,020  -->  00:13:04,160
you can do that by doing something like that.
268

268

00:13:04,160  -->  00:13:06,850
Self.name which is the name property inside self
269

269

00:13:06,850  -->  00:13:10,320
will take the value of the name variable
270

270

00:13:10,320  -->  00:13:13,710
inside this method which is this parameter here.
271

271

00:13:13,710  -->  00:13:17,818
So now if you print student.name you'll see that Bob
272

272

00:13:17,818  -->  00:13:21,493
will come out because that is what we defined
273

273

00:13:21,493  -->  00:13:23,370
in the init method.
274

274

00:13:23,370  -->  00:13:25,033
Let's do the same with grades.
275

275

00:13:26,900  -->  00:13:29,356
And now as well as name, we have to pass in
276

276

00:13:29,356  -->  00:13:32,420
a set of grades for the student.
277

277

00:13:32,420  -->  00:13:35,800
For example, we will do this one,
278

278

00:13:35,800  -->  00:13:37,950
give it just a little bit of a different set
279

279

00:13:37,950  -->  00:13:41,030
of grades there, and now we can press play
280

280

00:13:41,030  -->  00:13:43,950
and notice how the average grade changes.
281

281

00:13:43,950  -->  00:13:46,680
The average grade is now being calculated
282

282

00:13:46,680  -->  00:13:50,029
from this set of grades because they are stored
283

283

00:13:50,029  -->  00:13:54,400
in self.grades for this student.
284

284

00:13:54,400  -->  00:13:56,550
So when the average grade method is called
285

285

00:13:56,550  -->  00:13:58,510
it uses those grades.
286

286

00:13:58,510  -->  00:14:02,550
Remember that you can define multiple students if you want
287

287

00:14:02,550  -->  00:14:04,120
and that's totally fine.
288

288

00:14:04,120  -->  00:14:07,730
This call here in line 10 will call the init method,
289

289

00:14:07,730  -->  00:14:09,965
it will give you a new object, it will populate
290

290

00:14:09,965  -->  00:14:14,965
self.name of that object with the name argument
291

291

00:14:15,110  -->  00:14:19,530
and self.grades of the self object with this argument.
292

292

00:14:19,530  -->  00:14:22,780
And it will give you this student variable back.
293

293

00:14:22,780  -->  00:14:24,980
I'm gonna call this student two.
294

294

00:14:24,980  -->  00:14:27,670
The student two variable, exactly the same thing,
295

295

00:14:27,670  -->  00:14:30,600
calls the init method, passes Rolf as the name
296

296

00:14:30,600  -->  00:14:33,100
and it creates a new object and sets the name
297

297

00:14:33,100  -->  00:14:35,440
property of that new object to Rolf
298

298

00:14:35,440  -->  00:14:37,270
and the grades property of that new object
299

299

00:14:37,270  -->  00:14:39,220
to this set of grades here.
300

300

00:14:39,220  -->  00:14:41,410
And what you end up with are two separate
301

301

00:14:41,410  -->  00:14:43,240
containers of data.
302

302

00:14:43,240  -->  00:14:46,330
The student variable which is Bob and has these grades,
303

303

00:14:46,330  -->  00:14:49,242
and the student two variable which is Rolf
304

304

00:14:49,242  -->  00:14:50,075
and has these grades.
305

305

00:14:50,075  -->  00:14:52,733
So when you call student2.average_grade,
306

306

00:14:55,240  -->  00:14:58,973
again what Python is doing is it's calling the student class
307

307

00:14:58,973  -->  00:15:01,923
and passing student to there.
308

308

00:15:02,850  -->  00:15:05,770
So self is student two.
309

309

00:15:05,770  -->  00:15:08,940
When you sum the grades you're going to be summing
310

310

00:15:08,940  -->  00:15:11,300
these grades here because those are the grades
311

311

00:15:11,300  -->  00:15:14,109
that are stored inside student two.
312

312

00:15:14,109  -->  00:15:17,880
But of course you can still do student2.average_grade
313

313

00:15:17,880  -->  00:15:19,783
instead as that is a bit simpler.
314

314

00:15:21,110  -->  00:15:24,600
Pressing play now will give you 92.2 and 88.2
315

315

00:15:24,600  -->  00:15:28,440
as the two averages, one for each student.
316

316

00:15:28,440  -->  00:15:29,760
All right, that's everything for this video.
317

317

00:15:29,760  -->  00:15:31,250
I wanted to tell you a little bit about
318

318

00:15:31,250  -->  00:15:33,880
how object oriented programming works in Python,
319

319

00:15:33,880  -->  00:15:36,786
how you can define classes, how objects are created,
320

320

00:15:36,786  -->  00:15:40,160
and how methods work so that you can call them
321

321

00:15:40,160  -->  00:15:43,030
with the data in those objects.
322

322

00:15:43,030  -->  00:15:44,930
I hope it's all clear, but if it isn't,
323

323

00:15:44,930  -->  00:15:47,245
please ask a question in the course Q and A,
324

324

00:15:47,245  -->  00:15:48,780
we'll be more than happy to help you out.
325

325

00:15:48,780  -->  00:15:50,090
Thanks for joining me in this video
326

326

00:15:50,090  -->  00:15:51,740
and I'll see you in the next one.
