1
00:00:05,810 --> 00:00:08,650
Hi, welcome to this new lesson.

2
00:00:08,650 --> 00:00:10,550
We are going to continue talking

3
00:00:10,550 --> 00:00:12,330
about classes and how to really

4
00:00:12,330 --> 00:00:14,750
structure our files and

5
00:00:14,750 --> 00:00:17,230
how we can actually work
with multiple files,

6
00:00:17,230 --> 00:00:19,430
especially when we start
thinking of classes.

7
00:00:19,430 --> 00:00:22,230
Classes are very modular or

8
00:00:22,230 --> 00:00:25,550
something that makes
sense to almost package

9
00:00:25,550 --> 00:00:28,370
and have completely independent

10
00:00:28,370 --> 00:00:32,125
from the way we instantiate
classes in a particular code.

11
00:00:32,125 --> 00:00:34,230
Really thinking
of classes almost

12
00:00:34,230 --> 00:00:36,370
as libraries or
objects that we might

13
00:00:36,370 --> 00:00:41,950
deploy in our projects
at different times.

14
00:00:41,950 --> 00:00:44,870
Starts to reveal a little bit

15
00:00:44,870 --> 00:00:47,110
more of their modularity and a

16
00:00:47,110 --> 00:00:48,490
little bit more the way in which

17
00:00:48,490 --> 00:00:52,145
we can deploy their
power in a way.

18
00:00:52,145 --> 00:00:55,130
Let's just jump into the
code and discuss what

19
00:00:55,130 --> 00:00:59,035
actually we can do to
organize a class structure.

20
00:00:59,035 --> 00:01:01,650
Let's just remember
where we left off.

21
00:01:01,650 --> 00:01:03,985
We have this grading rectangle.

22
00:01:03,985 --> 00:01:07,400
This is a class that is
starting to get a bit long.

23
00:01:08,070 --> 00:01:14,390
I've used this series of dashes,

24
00:01:14,390 --> 00:01:16,950
this comment line to separate

25
00:01:16,950 --> 00:01:20,990
somehow the areas of
the code that refer to

26
00:01:20,990 --> 00:01:24,670
the class from something

27
00:01:24,670 --> 00:01:28,635
that could be the actual
implementation of the code,

28
00:01:28,635 --> 00:01:31,680
what we have been writing prior.

29
00:01:31,680 --> 00:01:34,110
Let's see how we
could actually open

30
00:01:34,110 --> 00:01:36,525
up a new tab here in processing.

31
00:01:36,525 --> 00:01:39,915
If we do here a new tab,

32
00:01:39,915 --> 00:01:46,330
and we could call it
a gradient rectangle.

33
00:01:50,990 --> 00:01:55,510
You see this new tap here,

34
00:01:55,930 --> 00:01:58,210
it's going to be

35
00:01:58,210 --> 00:02:01,650
a new area or a new
piece of code for us,

36
00:02:01,650 --> 00:02:05,430
and we're going to start
thinking of this area of

37
00:02:05,430 --> 00:02:08,190
code as dependency or

38
00:02:08,190 --> 00:02:09,885
something that
complements our code.

39
00:02:09,885 --> 00:02:16,535
Let's just take all the
code from these comments.

40
00:02:16,535 --> 00:02:19,320
Everything that has
to do with the class,

41
00:02:19,320 --> 00:02:21,980
we're going to cut it, and

42
00:02:21,980 --> 00:02:23,980
we're going to paste
it in this second tab.

43
00:02:23,980 --> 00:02:26,060
We're going to start
organizing our code.

44
00:02:26,060 --> 00:02:27,620
At this point, we
can actually get rid

45
00:02:27,620 --> 00:02:30,620
of these comments because
they're actually serving

46
00:02:30,620 --> 00:02:34,280
the purpose of the separation

47
00:02:34,280 --> 00:02:37,390
actually comes from
this different file.

48
00:02:38,540 --> 00:02:42,530
Let's just bring this back
and this is starting to look

49
00:02:42,530 --> 00:02:44,270
a lot more like a piece of code

50
00:02:44,270 --> 00:02:46,155
that we had created
without a class.

51
00:02:46,155 --> 00:02:48,430
But it makes use

52
00:02:48,430 --> 00:02:52,380
somewhere here of the
grading rectangle class.

53
00:02:52,380 --> 00:02:55,050
Let's see if we try to run this,

54
00:02:55,050 --> 00:02:57,945
we are not able to run that.

55
00:02:57,945 --> 00:03:01,690
Well, let's see how we could
actually access and import

56
00:03:01,690 --> 00:03:05,580
the information from the
gradient rectangle class.

57
00:03:05,580 --> 00:03:11,750
Let's do import and let's
use the name of the class.

58
00:03:11,860 --> 00:03:19,270
That was defined as and
we can actually give it

59
00:03:19,270 --> 00:03:27,870
a name as we could call it
GR, gradiate rectangle.

60
00:03:27,870 --> 00:03:29,810
The class name could be long,

61
00:03:29,810 --> 00:03:33,190
but if we want to actually
give it a different name here,

62
00:03:33,190 --> 00:03:38,250
we can import that with
a different name or we

63
00:03:38,250 --> 00:03:43,740
can actually just
keep it like that.

64
00:03:43,740 --> 00:03:46,770
Let's see, at this point,

65
00:03:46,770 --> 00:03:51,330
I'm going to give
you the name GR.

66
00:03:51,330 --> 00:03:58,250
Now, every time that I use
the gradient rectangle, call,

67
00:03:58,250 --> 00:04:04,070
I'm going to have to say
from the grading re art,

68
00:04:04,070 --> 00:04:06,750
gradient rectangle script, which

69
00:04:06,750 --> 00:04:09,960
is actually a different
script altogether.

70
00:04:10,400 --> 00:04:12,795
Use that class.

71
00:04:12,795 --> 00:04:17,115
That class belongs to
a different space.

72
00:04:17,115 --> 00:04:21,930
Belongs to a different script
that we are importing.

73
00:04:23,600 --> 00:04:26,000
Here, you see we could actually

74
00:04:26,000 --> 00:04:28,500
have everything
that we had before.

75
00:04:28,500 --> 00:04:32,460
But we're actually starting
to separate our files.

76
00:04:32,460 --> 00:04:34,120
If you really look at this tab,

77
00:04:34,120 --> 00:04:39,155
this tab here says the name
of the class dot Pi or PY,

78
00:04:39,155 --> 00:04:41,270
which stands for a Python file.

79
00:04:41,270 --> 00:04:43,130
This is actually
if you look into

80
00:04:43,130 --> 00:04:45,370
the folder where this
script is actually saved,

81
00:04:45,370 --> 00:04:48,935
you will see that you
have a class script.

82
00:04:48,935 --> 00:04:52,470
This is what we talk about
the boundaries of the class.

83
00:04:52,470 --> 00:04:54,370
The encapsulation of
the information of

84
00:04:54,370 --> 00:04:56,930
the class is a
self-contained object,

85
00:04:56,930 --> 00:04:59,150
and this self-contained
object could

86
00:04:59,150 --> 00:05:01,595
be used in multiple scripts.

87
00:05:01,595 --> 00:05:05,495
But basically, the class
might not need to change.

88
00:05:05,495 --> 00:05:07,530
This is, in fact,

89
00:05:07,530 --> 00:05:09,290
when you start really realizing

90
00:05:09,290 --> 00:05:11,090
how processing is
already written.

91
00:05:11,090 --> 00:05:12,890
Many of the functions,
for instance,

92
00:05:12,890 --> 00:05:14,910
the random attribute,

93
00:05:14,910 --> 00:05:16,890
we have been already
importing something.

94
00:05:16,890 --> 00:05:21,315
It's an inbuilt class that
is being provided for us.

95
00:05:21,315 --> 00:05:25,960
Similarly, a vector class
is being a class all along.

96
00:05:25,960 --> 00:05:27,460
They have been
objects that we have

97
00:05:27,460 --> 00:05:29,700
been using and deploying
within the code,

98
00:05:29,700 --> 00:05:32,460
and really if we try to
understand what they are,

99
00:05:32,460 --> 00:05:35,040
how they were constructed
in the first place,

100
00:05:35,040 --> 00:05:36,680
they were actually conceived as

101
00:05:36,680 --> 00:05:38,800
a class structure that
we are importing,

102
00:05:38,800 --> 00:05:41,330
and we don't need
to see that code

103
00:05:41,330 --> 00:05:43,410
necessarily because
processing already

104
00:05:43,410 --> 00:05:48,690
has its dependency to
the actual source.

105
00:05:48,690 --> 00:05:50,350
We are basically doing

106
00:05:50,350 --> 00:05:52,870
exactly the same thing
with our own class.

107
00:05:52,870 --> 00:05:56,210
We're inventing a class
called a gradient rectangle.

108
00:05:56,210 --> 00:05:58,410
We are importing it
into our system,

109
00:05:58,410 --> 00:06:00,410
and we can actually deploy it.

110
00:06:00,410 --> 00:06:04,290
This is a great power of
having multiple files.

111
00:06:04,290 --> 00:06:05,950
You can actually go ahead and

112
00:06:05,950 --> 00:06:11,450
construct an infinite
number of classes,

113
00:06:11,450 --> 00:06:15,750
and those could actually
be brought into your code.

114
00:06:15,750 --> 00:06:19,130
Without needing to have
a really long code

115
00:06:19,130 --> 00:06:22,130
that might be difficult to read.

116
00:06:22,130 --> 00:06:24,885
I really recommend you
to really start breaking

117
00:06:24,885 --> 00:06:29,660
your classes into different
very readable chunks.

118
00:06:29,660 --> 00:06:33,050
The final thing that I
would like to do as well is

119
00:06:33,050 --> 00:06:35,670
that sometimes whenever you

120
00:06:35,670 --> 00:06:37,930
have a class such as this one,

121
00:06:37,930 --> 00:06:41,430
that you think, well,

122
00:06:41,430 --> 00:06:43,330
I always want to be calling

123
00:06:43,330 --> 00:06:44,770
these three functions together,

124
00:06:44,770 --> 00:06:48,160
the calculate gradient,
the display, the move.

125
00:06:48,680 --> 00:06:50,865
Sometimes you will see

126
00:06:50,865 --> 00:06:54,810
this operation where you
have a function called run.

127
00:06:56,610 --> 00:06:59,925
That in itself invokes

128
00:06:59,925 --> 00:07:09,240
the self-dot calculate gradient,

129
00:07:15,190 --> 00:07:30,750
self-display, and self-dot move.

130
00:07:30,750 --> 00:07:33,310
This function is kind of
like a shortcut function.

131
00:07:33,310 --> 00:07:36,855
In case you want to
call this one instead.

132
00:07:36,855 --> 00:07:39,540
If we go back now
to our main class,

133
00:07:39,540 --> 00:07:40,820
you remember that we're calling

134
00:07:40,820 --> 00:07:42,640
these three functions every

135
00:07:42,640 --> 00:07:46,965
time we can save
that by saying run.

136
00:07:46,965 --> 00:07:50,250
Execute your internal
functionality

137
00:07:50,250 --> 00:07:53,940
and you'll see we
get the same result.

138
00:07:53,940 --> 00:07:59,840
But again, when you're operating
from within the class,

139
00:07:59,840 --> 00:08:05,085
you might want to specify
a series of behaviors,

140
00:08:05,085 --> 00:08:09,380
that compound to one single
functionality and this is

141
00:08:09,380 --> 00:08:11,380
a pattern that you
might see quite a bit

142
00:08:11,380 --> 00:08:13,160
where you could
actually decide, oh,

143
00:08:13,160 --> 00:08:17,980
this particular behavior,
it's really useful for

144
00:08:17,980 --> 00:08:20,820
us to be able to have an outline

145
00:08:20,820 --> 00:08:22,400
almost this run function becomes

146
00:08:22,400 --> 00:08:24,535
an outline of the
behaviors of this class.

147
00:08:24,535 --> 00:08:27,160
You'll see in future
sessions that we are going

148
00:08:27,160 --> 00:08:30,000
to spend a lot of
time in this outline.

149
00:08:30,000 --> 00:08:33,890
Because each function, could be

150
00:08:33,890 --> 00:08:36,090
thought in itself
as a contribution

151
00:08:36,090 --> 00:08:37,910
to a more complex behavior,

152
00:08:37,910 --> 00:08:39,770
and the outline of a class

153
00:08:39,770 --> 00:08:42,830
would really dictate
its expressivity,

154
00:08:42,830 --> 00:08:45,850
like the way it displays
itself in the screen.

155
00:08:45,850 --> 00:08:48,690
Mostly the call of that class is

156
00:08:48,690 --> 00:08:53,190
just a very simple call
for that class to exist,

157
00:08:53,190 --> 00:08:54,830
but we're actually going
to spend a lot of time in

158
00:08:54,830 --> 00:08:57,915
the outlining of the
behavior of the class.

159
00:08:57,915 --> 00:09:00,090
We're starting to get into

160
00:09:00,090 --> 00:09:02,305
what is object-oriented
thinking,

161
00:09:02,305 --> 00:09:06,160
how do we organize our
code so that we actually

162
00:09:06,160 --> 00:09:10,840
have a standalone class
that is very modular,

163
00:09:10,840 --> 00:09:13,120
and it's something that we
can reuse in many ways.

164
00:09:13,120 --> 00:09:14,440
How do we organize

165
00:09:14,440 --> 00:09:16,920
the code within a class
so that we can actually

166
00:09:16,920 --> 00:09:20,260
control its behavior
and really tinker and

167
00:09:20,260 --> 00:09:24,720
iterate over the results that
we're seeing on the screen.

168
00:09:24,720 --> 00:09:27,400
I'll invite you to use
this methodology for

169
00:09:27,400 --> 00:09:30,580
your design work
because it really

170
00:09:30,580 --> 00:09:33,840
maintains a very
structured organization

171
00:09:33,840 --> 00:09:35,420
of your code and as your code

172
00:09:35,420 --> 00:09:36,900
starts getting
longer and longer,

173
00:09:36,900 --> 00:09:40,480
you might want to have a
way of finding and really

174
00:09:40,480 --> 00:09:44,100
isolating the pieces that

175
00:09:44,100 --> 00:09:47,985
actually have the biggest
impact into your design.

176
00:09:47,985 --> 00:09:49,840
I hope that's helpful, and

177
00:09:49,840 --> 00:09:52,420
I'll see you in
the next session.