1
00:00:00,000 --> 00:00:04,900
2
00:00:04,900 --> 00:00:06,150
KENDRA PUGH: Hi.
3
00:00:06,150 --> 00:00:07,220
Let's talk about
object oriented
4
00:00:07,220 --> 00:00:08,810
programming in Python.
5
00:00:08,810 --> 00:00:10,260
First, object oriented
programming is
6
00:00:10,260 --> 00:00:11,370
a programming paradigm.
7
00:00:11,370 --> 00:00:14,170
It's in the same category as
things like functional
8
00:00:14,170 --> 00:00:16,280
programming and imperative
programming.
9
00:00:16,280 --> 00:00:18,730
But object oriented programming
is going to be the
10
00:00:18,730 --> 00:00:21,010
programming paradigm that
describes most of the code
11
00:00:21,010 --> 00:00:23,180
that you're going to interact
with in 6.01.
12
00:00:23,180 --> 00:00:25,100
So it's important to understand
how it works and
13
00:00:25,100 --> 00:00:28,620
how, in particular, you want
to be able to code in an
14
00:00:28,620 --> 00:00:31,860
object oriented programming
paradigm in Python.
15
00:00:31,860 --> 00:00:35,370
So today, I'm going to go over a
quick crash course on object
16
00:00:35,370 --> 00:00:37,840
oriented programming, and also
indicate all the little tips
17
00:00:37,840 --> 00:00:41,140
and tricks you need in order
to program in Python.
18
00:00:41,140 --> 00:00:42,900
Let's look at what I
have written up.
19
00:00:42,900 --> 00:00:47,930
So the most important thing to
remember when you're learning
20
00:00:47,930 --> 00:00:50,090
about the object oriented
programming paradigm is that
21
00:00:50,090 --> 00:00:51,480
everything is an object.
22
00:00:51,480 --> 00:00:53,480
And what I mean when I say that,
and what people mean
23
00:00:53,480 --> 00:01:00,510
when people say that, is that
the ideals behind the paradigm
24
00:01:00,510 --> 00:01:03,380
are that you interact with your
code in the same way that
25
00:01:03,380 --> 00:01:04,660
you would interact with
objects in the
26
00:01:04,660 --> 00:01:05,950
physical world, right?
27
00:01:05,950 --> 00:01:08,840
There's a particular piece
of paper on the
28
00:01:08,840 --> 00:01:09,920
desk in front of me.
29
00:01:09,920 --> 00:01:12,160
And it is a kind of
piece of paper.
30
00:01:12,160 --> 00:01:14,430
So I know how to interact with
it the way you would interact
31
00:01:14,430 --> 00:01:16,920
with any other piece of paper.
32
00:01:16,920 --> 00:01:19,400
If you want to codify this in an
object oriented programming
33
00:01:19,400 --> 00:01:21,650
paradigm, you write
up classes.
34
00:01:21,650 --> 00:01:24,200
Classes are your basic
unit of code block.
35
00:01:24,200 --> 00:01:30,150
They describe what a thing can
do and what a thing has or
36
00:01:30,150 --> 00:01:31,380
what attributes a thing has.
37
00:01:31,380 --> 00:01:33,290
And in object oriented
programming--
38
00:01:33,290 --> 00:01:35,320
frequently object oriented
programming and in object
39
00:01:35,320 --> 00:01:37,050
oriented programming
in Python--
40
00:01:37,050 --> 00:01:40,300
we refer to those things as
methods or functions that a
41
00:01:40,300 --> 00:01:43,810
particular object may have, and
attributes or particular
42
00:01:43,810 --> 00:01:47,100
variables that an
object may have.
43
00:01:47,100 --> 00:01:51,120
Once you've codified what any
object of a particular class
44
00:01:51,120 --> 00:01:54,590
can do, you can then use the
code that you've written to
45
00:01:54,590 --> 00:01:56,260
instantiate an object.
46
00:01:56,260 --> 00:02:01,060
An object is the functional unit
in the object oriented
47
00:02:01,060 --> 00:02:02,010
programming paradigm.
48
00:02:02,010 --> 00:02:04,010
It's the thing that you interact
with, and tell what
49
00:02:04,010 --> 00:02:07,080
to do, and produces
results for you.
50
00:02:07,080 --> 00:02:09,389
It's the thing that makes up--
51
00:02:09,389 --> 00:02:11,340
it and classes those are the
two things that you need to
52
00:02:11,340 --> 00:02:11,820
think about.
53
00:02:11,820 --> 00:02:15,330
But you also have to think about
how they're different.
54
00:02:15,330 --> 00:02:16,970
I have a particular sheet
of paper in front of me.
55
00:02:16,970 --> 00:02:19,040
It has all the properties
of a sheet of paper.
56
00:02:19,040 --> 00:02:21,010
And when I think about all the
things I can do to a sheet of
57
00:02:21,010 --> 00:02:23,120
paper, that constitutes
a class.
58
00:02:23,120 --> 00:02:25,630
But the particular piece of
paper that I have is an
59
00:02:25,630 --> 00:02:27,260
instance of that class.
60
00:02:27,260 --> 00:02:30,930
It's a particular
piece of paper.
61
00:02:30,930 --> 00:02:33,960
That's the gist of object
oriented programming and the
62
00:02:33,960 --> 00:02:35,640
things that you need to know.
63
00:02:35,640 --> 00:02:38,000
Now that I've covered them, I'm
going to go over the most
64
00:02:38,000 --> 00:02:40,135
basic class I could come up
with in terms of object
65
00:02:40,135 --> 00:02:42,710
oriented programming
in Python.
66
00:02:42,710 --> 00:02:47,600
This is a class that specifies
what a 6.01 staff member has
67
00:02:47,600 --> 00:02:55,270
in terms of an attribute
or a method in Python.
68
00:02:55,270 --> 00:03:01,480
If I want every instance of a
particular class or every
69
00:03:01,480 --> 00:03:04,910
staff member of the class, staff
member 6.01, to have a
70
00:03:04,910 --> 00:03:08,450
particular attribute, I
can specify like this.
71
00:03:08,450 --> 00:03:11,440
Every instance of Staff6.01
is going to have
72
00:03:11,440 --> 00:03:12,540
an attribute room.
73
00:03:12,540 --> 00:03:15,180
And that attribute room is going
to be set to the string
74
00:03:15,180 --> 00:03:18,980
describing 34-501,
the 6.01 lab.
75
00:03:18,980 --> 00:03:23,300
If I want every 6.01 staff
member to be able to do a
76
00:03:23,300 --> 00:03:25,660
particular thing, or have a
particular method, or call a
77
00:03:25,660 --> 00:03:29,610
particular function, then
I specify it like this.
78
00:03:29,610 --> 00:03:33,120
This is the beginning of a
method in the class Staff6.01.
79
00:03:33,120 --> 00:03:35,210
It's called sayHi.
80
00:03:35,210 --> 00:03:36,850
I'll talk about self
in a second.
81
00:03:36,850 --> 00:03:37,560
Don't worry about it.
82
00:03:37,560 --> 00:03:38,670
Act as though--
83
00:03:38,670 --> 00:03:43,030
if this is your introduction
to Python programming, then
84
00:03:43,030 --> 00:03:44,210
pretty much pretend
it's not there.
85
00:03:44,210 --> 00:03:48,280
It's kind of like this, but
we'll cover that in a second.
86
00:03:48,280 --> 00:03:53,500
And if any instance of the
Staff6.01 class calls sayHi,
87
00:03:53,500 --> 00:03:56,770
then "hello" will be printed
to standard out.
88
00:03:56,770 --> 00:03:59,310
I have a couple examples up
on the board behind me.
89
00:03:59,310 --> 00:04:02,020
And if you type them into Idle
and see what their return is
90
00:04:02,020 --> 00:04:04,510
like, you'll be able to--
91
00:04:04,510 --> 00:04:05,950
after you've typed in this--
92
00:04:05,950 --> 00:04:08,700
you'll be able to interact a
little bit better with what
93
00:04:08,700 --> 00:04:11,290
Python considers classes,
and objects, and
94
00:04:11,290 --> 00:04:12,540
that sort of thing.
95
00:04:12,540 --> 00:04:14,580
96
00:04:14,580 --> 00:04:18,300
If you look at type Staff6.01,
it'll tell you about a class,
97
00:04:18,300 --> 00:04:19,800
which is an object in itself.
98
00:04:19,800 --> 00:04:24,730
But it's a specification for
instances of an object.
99
00:04:24,730 --> 00:04:28,900
If you want to instantiate
an object that is of type
100
00:04:28,900 --> 00:04:32,370
Staff6.01, you need to use the
parenthesess on the end.
101
00:04:32,370 --> 00:04:37,090
This treats Staff6.01 like a
call and creates an object.
102
00:04:37,090 --> 00:04:39,610
If you just type Staff6.01,
you're just reassigning
103
00:04:39,610 --> 00:04:43,010
Staff6.01 class to the name
kpugh, and that's not useful.
104
00:04:43,010 --> 00:04:44,610
Every Staff6.01 member
should not be
105
00:04:44,610 --> 00:04:48,860
considered a Kendra, right?
106
00:04:48,860 --> 00:04:53,060
Once you have instantiated a
particular object of type
107
00:04:53,060 --> 00:04:56,570
Staff6.01, you can look at the
type of that object, right?
108
00:04:56,570 --> 00:05:00,230
Now you've got one object,
kpugh, myself.
109
00:05:00,230 --> 00:05:04,200
And that is a class
of Staff6.01.
110
00:05:04,200 --> 00:05:07,400
Likewise, now that you have this
object, kpugh, you can
111
00:05:07,400 --> 00:05:09,090
look at its attributes
and methods.
112
00:05:09,090 --> 00:05:11,940
If you look at kpugh.room, then
it should print to the
113
00:05:11,940 --> 00:05:17,460
screen "34-501." That's because
that's the attribute
114
00:05:17,460 --> 00:05:19,750
associated with this instance.
115
00:05:19,750 --> 00:05:26,250
If you call kpugh.sayHi(), it
will use the method in the
116
00:05:26,250 --> 00:05:28,860
class type of this object.
117
00:05:28,860 --> 00:05:32,770
So when you call .sayHi(), it
looks at kpugh, looks at the
118
00:05:32,770 --> 00:05:36,510
type, says, that's of type
Staff6.01, goes to class
119
00:05:36,510 --> 00:05:39,790
Staff6.01, finds the definition
for sayHi(), and
120
00:05:39,790 --> 00:05:41,640
then executes this code.
121
00:05:41,640 --> 00:05:43,040
Hopefully, that all
made sense.
122
00:05:43,040 --> 00:05:45,560
123
00:05:45,560 --> 00:05:48,480
Now, I want to talk
about self.
124
00:05:48,480 --> 00:05:52,590
And you might say, Kendra, I
don't understand where that
125
00:05:52,590 --> 00:05:53,400
comes into play.
126
00:05:53,400 --> 00:05:55,790
And you didn't even
use it over here!
127
00:05:55,790 --> 00:05:59,880
If you're familiar with C++ or
Java, self is a lot like this.
128
00:05:59,880 --> 00:06:02,980
Self is an implicit argument
passed in here.
129
00:06:02,980 --> 00:06:05,830
Even though you specify zero
arguments, it's considered the
130
00:06:05,830 --> 00:06:06,850
first argument.
131
00:06:06,850 --> 00:06:09,090
And you'll probably see a lot
type errors when you're first
132
00:06:09,090 --> 00:06:10,910
programming in object oriented
programming that say that
133
00:06:10,910 --> 00:06:13,580
you've either passed in too
many or too few arguments.
134
00:06:13,580 --> 00:06:18,340
It has to do with this
definition, with self.
135
00:06:18,340 --> 00:06:22,750
Self says, I am talking
about myself.
136
00:06:22,750 --> 00:06:24,220
That's not particularly
intuitive.
137
00:06:24,220 --> 00:06:26,580
But I'll try to explain
a little bit more.
138
00:06:26,580 --> 00:06:33,850
When kpugh calls .sayHi(),
sayHi() always has an implicit
139
00:06:33,850 --> 00:06:37,810
reference to whatever
called it.
140
00:06:37,810 --> 00:06:42,890
When you look at this code,
other instances can call this
141
00:06:42,890 --> 00:06:43,520
code, right?
142
00:06:43,520 --> 00:06:47,370
If I had an instance of Adam
Hartz or an instance of Ike
143
00:06:47,370 --> 00:06:54,380
Chuang they would also have
access to the method sayHi().
144
00:06:54,380 --> 00:06:58,040
And when they called sayHi(),
sayHi() would point back to
145
00:06:58,040 --> 00:07:01,700
the class definition, but also
have a reference to whatever
146
00:07:01,700 --> 00:07:03,500
instance called it.
147
00:07:03,500 --> 00:07:09,530
So when you substitute in this
self, you substitute in
148
00:07:09,530 --> 00:07:12,900
whatever instance called
the method.
149
00:07:12,900 --> 00:07:15,225
That doesn't seem particularly
useful right now because that
150
00:07:15,225 --> 00:07:18,910
class definition does not
actually make any use of the
151
00:07:18,910 --> 00:07:25,830
self, or the ability to use
unique instances of an object
152
00:07:25,830 --> 00:07:30,220
as sort of unique storage
containers.
153
00:07:30,220 --> 00:07:30,910
I'm kpugh.
154
00:07:30,910 --> 00:07:33,520
I'm different from Adam Hartz.
155
00:07:33,520 --> 00:07:36,090
And therefore, I should be
able to have different
156
00:07:36,090 --> 00:07:38,720
attributes, or different
methods, or things that act
157
00:07:38,720 --> 00:07:42,370
slightly differently from the
way they do for Adam.
158
00:07:42,370 --> 00:07:45,140
I'm going to look at a revised
definition of class Staff6.01,
159
00:07:45,140 --> 00:07:50,180
and that definition will use
self in a way that indicates
160
00:07:50,180 --> 00:07:55,750
that you can have different
functionality
161
00:07:55,750 --> 00:07:57,000
for different instances.
162
00:07:57,000 --> 00:07:59,300
163
00:07:59,300 --> 00:08:01,870
It's right here.
164
00:08:01,870 --> 00:08:03,650
Class Staff6.01--
165
00:08:03,650 --> 00:08:04,880
it looks really similar.
166
00:08:04,880 --> 00:08:07,360
In fact, the first two lines
are exactly the same.
167
00:08:07,360 --> 00:08:08,910
We've got a class attribute.
168
00:08:08,910 --> 00:08:12,180
That means that every instance
of this class is going to have
169
00:08:12,180 --> 00:08:15,850
this attribute because it's
a class attribute.
170
00:08:15,850 --> 00:08:19,390
If I want different
instantiations of my class to
171
00:08:19,390 --> 00:08:23,760
have different properties,
then I need to explicitly
172
00:08:23,760 --> 00:08:26,940
address the initialization
of those properties.
173
00:08:26,940 --> 00:08:29,780
In Python, when we want
to do that, we
174
00:08:29,780 --> 00:08:31,750
define the method __init__.
175
00:08:31,750 --> 00:08:33,380
__init__ is a very
special method.
176
00:08:33,380 --> 00:08:35,980
It's got these underscores.
177
00:08:35,980 --> 00:08:40,669
I think it's a protected
keyword.
178
00:08:40,669 --> 00:08:43,830
And it always has the format
"self," and then whatever
179
00:08:43,830 --> 00:08:45,890
arguments you want to
pass in when you're
180
00:08:45,890 --> 00:08:47,140
instantiating an object.
181
00:08:47,140 --> 00:08:50,140
182
00:08:50,140 --> 00:08:51,910
__init__ is not exactly
a constructor.
183
00:08:51,910 --> 00:08:56,210
But for those of you that are
familiar with C++ and Java, it
184
00:08:56,210 --> 00:08:57,730
acts like a constructor.
185
00:08:57,730 --> 00:09:01,310
Immediately after the object is
186
00:09:01,310 --> 00:09:03,200
constructed, __init__ is called.
187
00:09:03,200 --> 00:09:05,140
And all the set up that
is required to set
188
00:09:05,140 --> 00:09:07,660
up the object happens.
189
00:09:07,660 --> 00:09:11,010
So any time you instantiate an
object, all of these things
190
00:09:11,010 --> 00:09:11,830
are going to be executed.
191
00:09:11,830 --> 00:09:13,200
Or all the things
under __init__
192
00:09:13,200 --> 00:09:15,290
is going to be executed.
193
00:09:15,290 --> 00:09:19,800
In particular, we're going to
set the attribute greeting to
194
00:09:19,800 --> 00:09:23,150
whatever argument we
passed in when we
195
00:09:23,150 --> 00:09:24,400
instantiated the object.
196
00:09:24,400 --> 00:09:26,840
197
00:09:26,840 --> 00:09:30,470
So every instance that we create
of this class is going
198
00:09:30,470 --> 00:09:32,360
to have the class
attribute, room.
199
00:09:32,360 --> 00:09:34,580
They're all going to be
in the same room.
200
00:09:34,580 --> 00:09:35,860
But they're also going
to have a greeting.
201
00:09:35,860 --> 00:09:38,060
And you have the option of
specifying the greeting to be
202
00:09:38,060 --> 00:09:40,920
whatever you want.
203
00:09:40,920 --> 00:09:45,020
We're going to make use of this
in the method sayHi(),
204
00:09:45,020 --> 00:09:47,910
which still only takes the
argument self, or the
205
00:09:47,910 --> 00:09:51,560
reference to whatever object
called the method
206
00:09:51,560 --> 00:09:54,080
in the first place.
207
00:09:54,080 --> 00:09:57,640
That reference is going to
get substituted in here.
208
00:09:57,640 --> 00:10:03,050
So no matter which object calls
the method, you will
209
00:10:03,050 --> 00:10:08,240
have access to its particular
greeting using this syntax.
210
00:10:08,240 --> 00:10:09,490
Let's walk through an example.
211
00:10:09,490 --> 00:10:12,200
212
00:10:12,200 --> 00:10:16,470
Let's say I make an example
of Adam Hartz, and he is a
213
00:10:16,470 --> 00:10:17,410
Staff6.01 member.
214
00:10:17,410 --> 00:10:21,470
And his greeting is going to be
"hi." Likewise, let's make
215
00:10:21,470 --> 00:10:25,330
another instance of me using the
new Staff6.01 definition.
216
00:10:25,330 --> 00:10:26,600
Make sure you type this
in because it's not
217
00:10:26,600 --> 00:10:29,090
going to work otherwise.
218
00:10:29,090 --> 00:10:34,770
And make my greeting "HELLO,"
as opposed to just "hi."
219
00:10:34,770 --> 00:10:40,820
If you call the sayHi() method
using hartz, then you should
220
00:10:40,820 --> 00:10:42,850
get a different result then
when you call the sayHi()
221
00:10:42,850 --> 00:10:45,290
method using kpugh.
222
00:10:45,290 --> 00:10:48,060
But if you call the
room method--
223
00:10:48,060 --> 00:10:49,630
or, excuse me.
224
00:10:49,630 --> 00:10:54,700
If you were after the room
attribute of both instances,
225
00:10:54,700 --> 00:10:56,940
then you should get the same
result because this is the
226
00:10:56,940 --> 00:11:02,690
class attribute definition,
whereas this attribute is
227
00:11:02,690 --> 00:11:03,940
specific to each instance.
228
00:11:03,940 --> 00:11:07,370
229
00:11:07,370 --> 00:11:09,210
That's all I have to say for
now about object oriented
230
00:11:09,210 --> 00:11:09,840
programming.
231
00:11:09,840 --> 00:11:12,290
In my next video, I'll start
to talk about inheritance,
232
00:11:12,290 --> 00:11:15,430
which is another really
important property in 6.01 and
233
00:11:15,430 --> 00:11:18,060
also object oriented programming
in Python, and
234
00:11:18,060 --> 00:11:18,920
also has some slip ups.
235
00:11:18,920 --> 00:11:20,840
So I'd like to talk to
you about those next.
236
00:11:20,840 --> 00:11:22,090