Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
1
00:00:05,560 --> 00:00:09,580
so there's several different programming
paradigms and up until now in this
2
2
00:00:09,580 --> 00:00:13,070
course we've been mainly using the
imperative style of programming
3
3
00:00:13,070 --> 00:00:17,740
imperative programming involves
providing a series of instructions for
4
4
00:00:17,740 --> 00:00:22,140
the computer to follow in a defined
order now object oriented programming
5
5
00:00:22,140 --> 00:00:28,060
aims to combine data and the processes that
act on that data into objects which is
6
6
00:00:28,060 --> 00:00:30,090
called encapsulation
7
7
00:00:30,090 --> 00:00:34,020
before I continue it's important to
realize that the different paradigms are
8
8
00:00:34,020 --> 00:00:38,180
not necessarily exclusive and
object-oriented programming makes use of
9
9
00:00:38,180 --> 00:00:42,050
imperative programming within the
methods that objects used to manipulate
10
10
00:00:42,050 --> 00:00:48,070
the data also as we will see shortly our
imperative programming in Python has
11
11
00:00:48,070 --> 00:00:53,160
actually made extensive use of objects
we just didn't know that so the approach that
12
12
00:00:53,160 --> 00:00:58,190
we've been using so far in the course is
very similar to a recipe for cooking a
13
13
00:00:58,190 --> 00:01:02,340
meal so the recipe starts with the
ingredients and utensils it will be need
14
14
00:01:02,340 --> 00:01:07,100
which corresponds to a programs data
and then continues with the description of
15
15
00:01:07,100 --> 00:01:11,520
the steps that must be performed on that
data to produce in this case for the
16
16
00:01:11,520 --> 00:01:12,260
recipe
17
17
00:01:12,260 --> 00:01:17,120
finished meal an object oriented
approach relies on the objects such as
18
18
00:01:17,120 --> 00:01:22,260
the eggs milk spoon etc knowing
how to perform certain operations so
19
19
00:01:22,260 --> 00:01:24,640
the program just tells them to do so
20
20
00:01:24,640 --> 00:01:29,630
concept of an egg that knows how to fry
itself is a little bizarre but that
21
21
00:01:29,630 --> 00:01:33,190
really shows that the imperatives
approach to cooking a meal is more
22
22
00:01:33,190 --> 00:01:36,190
appropriate than an object-oriented
approach
23
23
00:01:36,910 --> 00:01:40,900
now a good example of the encapsulation of
data and function might be self lighting
24
24
00:01:40,900 --> 00:01:44,310
cigarettes well actually its a
bad example and I certainly wouldn't
25
25
00:01:44,310 --> 00:01:48,840
want to suggest that smoking is good but
it does demonstrate the concept quite well
26
26
00:01:48,840 --> 00:01:53,720
so without object oriented programming
the steps to smoking a cigarette might
27
27
00:01:53,720 --> 00:01:58,540
be place cigarette in mouth that's number
one number two light match number three
28
28
00:01:58,540 --> 00:02:03,650
hold match to cigarette and number four
inhale now encapsulating the functions
29
29
00:02:03,650 --> 00:02:08,460
that can be performed on the cigarette
into the cigarette reduces the code to
30
30
00:02:08,460 --> 00:02:14,230
place cigarette in mouth and inhale so
the cigarette knows how to light itself so
31
31
00:02:14,230 --> 00:02:17,190
that's not something that the smoker
needs to be concerned with
32
32
00:02:17,190 --> 00:02:21,960
the details of how it lights itself are
built into it when it's manufactured or
33
33
00:02:21,960 --> 00:02:25,610
in the case of programming it's
programmed into it when it's defined
34
34
00:02:25,610 --> 00:02:30,250
and the actual implementation is of
no interest to anything that uses the
35
35
00:02:30,250 --> 00:02:34,470
object now I've exercise a little license
they're self lighting cigarettes were
36
36
00:02:34,470 --> 00:02:38,990
produced for short time the 1960's
coincidentally around the same time that
37
37
00:02:38,990 --> 00:02:43,280
object oriented programming was
conceived and had a igniter
38
38
00:02:43,280 --> 00:02:48,310
built into them that had to be struck like a match
the advanced type that I described only
39
39
00:02:48,310 --> 00:02:53,380
existed in science fiction stories now
modern electronic cigarettes take this
40
40
00:02:53,380 --> 00:02:56,790
concept a little bit further but it's
only the details of the implementation
41
41
00:02:56,790 --> 00:03:01,560
that have changed so the method use
remains pretty much the same as the
42
42
00:03:01,560 --> 00:03:06,690
science-fiction variety now that's important
concept of object oriented
43
43
00:03:06,690 --> 00:03:11,030
programming as we will see replacing one
object with another one that performs
44
44
00:03:11,030 --> 00:03:15,330
the same task without having to concern
ourselves with the details of how it
45
45
00:03:15,330 --> 00:03:20,850
performs the task is central to object
oriented programming so another and
46
46
00:03:20,850 --> 00:03:25,170
hopefully much more healthier example
might be an electric kettle now the
47
47
00:03:25,170 --> 00:03:29,980
imperative approach to boiling water
for hot drink might be step 1 fill kettle with
48
48
00:03:29,980 --> 00:03:30,570
water
49
49
00:03:30,570 --> 00:03:37,360
step 2 light stove step 3 place kettle on stove step 4 for monitor kettle until
50
50
00:03:37,360 --> 00:03:41,970
water boils step 5 make the drink now it would then have to be a function that
51
51
00:03:41,970 --> 00:03:45,610
deals with lighting the stove which might
involve further functions to collect
52
52
00:03:45,610 --> 00:03:50,780
water or coal or was switched on the gas a
function to light the match and so on but
53
53
00:03:50,780 --> 00:03:55,450
in object orientated terms a kettle would have a
boil method that is triggered by
54
54
00:03:55,450 --> 00:03:59,890
operating a switch so the object
oriented program comes down to step
55
55
00:03:59,890 --> 00:04:01,320
one fill kettle with water
56
56
00:04:01,320 --> 00:04:07,630
step 2 switch kettle on step 3 when kettle switches off make drink so the
57
57
00:04:07,630 --> 00:04:11,030
imperative approach will also have to
identify the type of stove being used as
58
58
00:04:11,030 --> 00:04:15,290
its futile and probably a bit dangerous to hold a match to electric cooker
59
59
00:04:15,290 --> 00:04:20,040
now these examples may make it look like
we've got something for nothing because
60
60
00:04:20,040 --> 00:04:23,910
performing the tasks using an object
oriented approach involves fewer steps
61
61
00:04:23,910 --> 00:04:28,970
but there's no magic involved here now the
steps are still had to be programmed
62
62
00:04:28,970 --> 00:04:33,280
but their built into the objects
so that anything that using the object doesn't have to be
63
63
00:04:33,280 --> 00:04:37,970
concerned about them so the object orientated kettle has been built to contain an
64
64
00:04:37,970 --> 00:04:41,950
element or whatever method it employs could
be a ceramic disc or even a small
65
65
00:04:41,950 --> 00:04:46,600
nuclear reactor and the operation of
the heating mechanism has been wired up to
66
66
00:04:46,600 --> 00:04:51,180
the switch where the kettle was made so a lot
of work has gone into the production on
67
67
00:04:51,180 --> 00:04:55,370
the kettle in other words far more so that
the work that goes into making a kettle
68
68
00:04:55,370 --> 00:04:59,370
for use on the stove but subsequent use
of the kettle is there much easier
69
69
00:04:59,370 --> 00:05:04,010
because it knows how to perform its
function itself so one thing that may
70
70
00:05:04,010 --> 00:05:09,660
not be obvious and I haven't yet mentioned is that
everything in Python is an object so Python
71
71
00:05:09,660 --> 00:05:15,380
is unusual in this respect because even though Python uses objects extensively you
72
72
00:05:15,380 --> 00:05:19,480
don't actually have to be aware of that
to use so Python can be used for
73
73
00:05:19,480 --> 00:05:24,010
object-oriented programming but can also
be used to write purely imperative
74
74
00:05:24,010 --> 00:05:29,350
programming code so in fact the
Wikipedia entry for Python which I'm going to just load
75
75
00:05:29,350 --> 00:05:37,320
up the screen now you can see the link
in the top left hand corner you can see
76
76
00:05:37,320 --> 00:05:42,590
just down here it says that Python
supports multiple programming paradigms
77
77
00:05:42,590 --> 00:05:46,650
including object oriented imperative and
functional programming or procedural
78
78
00:05:46,650 --> 00:05:52,330
styles so in Java which is considered a
class-based object-oriented language
79
79
00:05:52,330 --> 00:05:54,760
there are primitive types such as an
80
80
00:05:54,760 --> 00:05:59,070
int and a double so if you've come to Python
from Java the following code which I'm
81
81
00:05:59,070 --> 00:06:05,040
about to show might seem very odd at
first so lets just go back to IntelliJ and I've created a
82
82
00:06:05,040 --> 00:06:10,350
new project here and a new Python file so if
you come back come from Java type in
83
83
00:06:10,350 --> 00:06:20,720
code like this....
84
84
00:06:20,720 --> 00:06:30,150
....so if we just run this see that it does
85
85
00:06:30,150 --> 00:06:38,070
work and we get the same result each time so line 3...and
86
86
00:06:38,070 --> 00:06:41,190
that's pretty normal for a programming
language like Java
87
87
00:06:41,190 --> 00:06:47,640
but the code on line 4 would probably be a bit bizarre and that's because the a even though
88
88
00:06:47,640 --> 00:06:52,060
it's been defined as you can see on line
12 as a number you could see that a has
89
89
00:06:52,060 --> 00:06:56,180
got an add method even though its an integer so if I command click add
90
90
00:06:57,210 --> 00:07:02,540
and that is control click on a PC or Linux you can
see that there's a method this is the
91
91
00:07:02,540 --> 00:07:07,600
built-ins definition of the add method and
interestingly enough if I go back to oop
92
92
00:07:07,600 --> 00:07:14,330
our file and click on the plus note that it has gone to the same method in the built ins
93
93
00:07:14,330 --> 00:07:19,200
so what you can see by that is that whether
you use this __add__
94
94
00:07:19,200 --> 00:07:23,760
or use a plus it's calling
the same code that is built-in
95
95
00:07:23,760 --> 00:07:29,390
builtins.py file and in this case add
two numbers together and smart enough to
96
96
00:07:29,390 --> 00:07:33,440
figure out the parameters you can see
there that the second argument in the
97
97
00:07:33,440 --> 00:07:38,740
case of line 3 is then added and line 4
the parameters b is added to the value
98
98
00:07:38,740 --> 00:07:43,570
of a now if this is your first
programming language and either you see
99
99
00:07:43,570 --> 00:07:47,650
nothing at all strange about this
or it will make very little sense but don't worry
100
100
00:07:47,650 --> 00:07:52,080
I'm gonna explain what it all means but
I wanted to warn you that you know if
101
101
00:07:52,080 --> 00:07:56,690
you've come from other programming
languages such as Java or C++ you
102
102
00:07:56,690 --> 00:08:00,450
probably gonna see some aspects of Python that seem surprising to you at
103
103
00:08:00,450 --> 00:08:04,210
least initially now when I said that
everything in Python is an object I meant
104
104
00:08:04,210 --> 00:08:09,340
everything even types are
implemented as classes now I will come
105
105
00:08:09,340 --> 00:08:13,100
back to that but for now I'm gonna focus
on making sense of what I've just done
106
106
00:08:13,100 --> 00:08:19,250
their so object-oriented programming uses
classes and methods to provide object
107
107
00:08:19,250 --> 00:08:24,310
that encapsulates both data and the
functions that operate on that Data now the
108
108
00:08:24,310 --> 00:08:28,030
method is just another word for function
and you have seen me use those terms
109
109
00:08:28,030 --> 00:08:32,940
interchangeably in this course but being big
correct here when a function is part of
110
110
00:08:32,940 --> 00:08:37,350
a class in Python we call it a method
now in Python there is a slight
111
111
00:08:37,350 --> 00:08:41,320
difference between a function and a method but writing a method is the same
112
112
00:08:41,320 --> 00:08:45,660
as writing functions now what we gonna
do is start with a very simple example
113
113
00:08:45,660 --> 00:08:51,060
to introduce the concept of a class and
show how classes work in Python now this
114
114
00:08:51,060 --> 00:08:53,920
example be a simple implementation of
the electric
115
115
00:08:53,920 --> 00:08:58,260
kettle so let's go ahead and do that's
so close the run window and I'm just gonna
116
116
00:08:58,260 --> 00:09:05,380
delete this code and we start by creating
a kettle class and its gonna model an
117
117
00:09:05,380 --> 00:09:06,510
electric kettle
118
118
00:09:06,510 --> 00:09:16,950
so.....and notice how its has been indented for us
119
119
00:09:16,950 --> 00:09:19,950
automatically and we're going to create a
method so....
120
120
00:09:21,130 --> 00:09:29,440
....
121
121
00:09:29,440 --> 00:09:32,320
....will talk about this shortly...
122
122
00:09:32,320 --> 00:09:39,490
we just type in the code for now so...
123
123
00:09:39,490 --> 00:09:42,490
....
124
124
00:09:43,390 --> 00:09:50,070
...so looking at this it can be helpful to
think of a class as a template from
125
125
00:09:50,070 --> 00:09:55,150
which objects can be created so when we
create objects of this kettle class they
126
126
00:09:55,150 --> 00:10:00,290
all have a name and a price now they won't
have the same name nor the same price
127
127
00:10:00,290 --> 00:10:06,390
each instance of the class will have its
own values for name and price so classes
128
128
00:10:06,390 --> 00:10:10,650
as I said is a template from which objects
can be created and all the objects
129
129
00:10:10,650 --> 00:10:15,670
created from the same class will share
the same characteristics now an instance
130
130
00:10:15,670 --> 00:10:21,760
is just another name for an object created from a
class definition so if I now create a
131
131
00:10:21,760 --> 00:10:27,480
kettle called Kenwood then Kenwood will be an
instance of the kettle class you can also
132
132
00:10:27,480 --> 00:10:32,440
say that kenwood is an object of
type kettle so I'm gonna create a couple of
133
133
00:10:32,440 --> 00:10:37,050
kettle objects so we can see this in
practice so let's continue typing and
134
134
00:10:37,050 --> 00:10:45,190
we will go to line 8 here and type...
135
135
00:10:45,190 --> 00:11:11,950
....
136
136
00:11:11,950 --> 00:11:18,320
.....
137
137
00:11:18,320 --> 00:11:22,770
.....and lets make another kettle...
138
138
00:11:22,770 --> 00:11:35,290
...and I should really have two lines there
139
139
00:11:35,290 --> 00:11:39,700
separating the class definition to be
consistent with our code so the code on
140
140
00:11:39,700 --> 00:11:45,290
line 9 creates an instance of the kettle
class and we've given it the name
141
141
00:11:45,290 --> 00:11:49,750
kenwood and other instances is created on line 16 and this time it's called the
142
142
00:11:49,750 --> 00:11:55,230
Hamilton so each instance has its own
values for naming and price and their access
143
143
00:11:55,230 --> 00:12:00,200
by using . annotations so we type in Kenwood.price or Hamilton.make for
144
144
00:12:00,200 --> 00:12:06,240
argument sake to access that information
so a good analogy here is for plans for
145
145
00:12:06,240 --> 00:12:10,880
building a house so the plan itself just
describes what the house will look like
146
146
00:12:10,880 --> 00:12:15,340
its size how many rooms it will have etc
now this is similar
147
147
00:12:15,340 --> 00:12:20,060
the plan to the class which is just a
template for creating objects once you
148
148
00:12:20,060 --> 00:12:23,940
have a plan you can then create it as many
houses as you want based on that
149
149
00:12:23,940 --> 00:12:29,170
plan and the same is true of classes
once a class has been defined like how
150
150
00:12:29,170 --> 00:12:33,630
kettle class that we define on line one
we can then create as many instances of that
151
151
00:12:33,630 --> 00:12:38,340
classes as we want now just as you can't do
anything with the house plan other than
152
152
00:12:38,340 --> 00:12:42,330
build houses based on it you can't
really do anything with the class other
153
153
00:12:42,330 --> 00:12:47,360
than create instances of it once the
instances have been created you can then call
154
154
00:12:47,360 --> 00:12:53,210
their methods and access their variables so
on line 11 Kenwood.price retrieves
155
155
00:12:53,210 --> 00:12:58,260
the price from the instance called
Kenwood and Hamilton.price will get the
156
156
00:12:58,260 --> 00:13:03,490
price of the Hamilton instance we can
also give the price a new value as we see on line
157
157
00:13:03,490 --> 00:13:10,480
13 where we set the price to $12.75 so we've been
using dot notation extensively in our
158
158
00:13:10,480 --> 00:13:12,050
programs in this course
159
159
00:13:12,050 --> 00:13:16,000
when you seen that when we've used the
append method to add items to a list for example
160
160
00:13:16,000 --> 00:13:22,070
also all the TK widgets are objects
that have .pack and grid methods amongst
161
161
00:13:22,070 --> 00:13:26,640
many others to allow them to be placed
in the window manager so lets add another
162
162
00:13:26,640 --> 00:13:31,380
example in this program so I'm going to start typing and put on some code on line 18
163
163
00:13:31,380 --> 00:13:34,570
gonna type...
164
164
00:13:46,250 --> 00:13:59,400
.....
165
165
00:13:59,400 --> 00:14:11,670
....and you can see here models kenwood equals$12.75 and Hamilton equals $14.55 so it's
166
166
00:14:11,670 --> 00:14:16,080
retrieved the values out of each
instance of the 2 kettle objects that
167
167
00:14:16,080 --> 00:14:22,690
we've created so the parameters in the format
method are the make price data attributes of
168
168
00:14:22,690 --> 00:14:26,630
the Kenwood and Hamilton objects
another bit of jargon here the
169
169
00:14:26,630 --> 00:14:30,700
attributes so we've become used to
talking about variables but when a
170
170
00:14:30,700 --> 00:14:35,020
variable is bound to an instance of a
class then it's referred to as a data
171
171
00:14:35,020 --> 00:14:39,740
attribute in Python now their other
object-oriented programming terms for
172
172
00:14:39,740 --> 00:14:44,110
the same things you might find variable
attributes also called fields in
173
173
00:14:44,110 --> 00:14:49,600
languages such as Java or data members
in C++ now I'll introduced yet another term
174
174
00:14:49,600 --> 00:14:53,320
shortly probably in the next video one
that's been borrowed from the small talk
175
175
00:14:53,320 --> 00:14:58,940
language but lets end this video here and in then next
video we'll continue our discussion about
176
176
00:14:58,940 --> 00:15:00,460
objects see you in the next video
20795
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.