WEBVTT

1
00:00:00.450 --> 00:00:01.590
<v Jose>Hi and welcome back.</v>

2
00:00:01.590 --> 00:00:06.030
In this video we're going to talk about property setters.

3
00:00:06.030 --> 00:00:09.460
Let's create a new file that I'm gonna call Flight,

4
00:00:10.640 --> 00:00:13.880
and in here I'm going to just define a simple class

5
00:00:13.880 --> 00:00:17.720
that will be a flight object.

6
00:00:18.560 --> 00:00:19.670
In case you don't know,

7
00:00:19.670 --> 00:00:23.660
flights are normally something that looks like this.

8
00:00:23.660 --> 00:00:26.120
It goes for example from Glasgow

9
00:00:26.120 --> 00:00:27.790
to London Heathrow.

10
00:00:28.700 --> 00:00:30.710
This could be a flight,

11
00:00:31.630 --> 00:00:35.380
and in this flight you have one segment.

12
00:00:35.380 --> 00:00:38.890
A segment is essentially you getting on a plane,

13
00:00:38.890 --> 00:00:41.670
and you landing somewhere else.

14
00:00:41.670 --> 00:00:44.210
If you're going from Glasgow to Toronto,

15
00:00:44.210 --> 00:00:46.910
you'll actually stop in London Heathrow in the middle.

16
00:00:48.830 --> 00:00:50.400
So you'll have something like this,

17
00:00:50.400 --> 00:00:52.590
Glasgow to London Heathrow to Toronto.

18
00:00:54.450 --> 00:00:56.870
So these are two segments.

19
00:00:57.740 --> 00:01:00.780
And the whole thing is known as a flight leg,

20
00:01:00.780 --> 00:01:05.510
so flight leg in this case has two segments.

21
00:01:05.510 --> 00:01:07.400
Glasgow to London Heathrow,

22
00:01:07.400 --> 00:01:09.530
and London Heathrow to Toronto.

23
00:01:10.970 --> 00:01:12.480
Just a bit of background information,

24
00:01:12.480 --> 00:01:14.350
I am sort of getting a bit

25
00:01:16.320 --> 00:01:19.090
disillusioned with the animals and the friend example,

26
00:01:19.090 --> 00:01:21.170
so now that you know a bit more about Python,

27
00:01:21.170 --> 00:01:24.790
I wanted to give you some more interesting examples

28
00:01:24.790 --> 00:01:26.760
of things you can do.

29
00:01:26.760 --> 00:01:29.220
So let's create this flight class.

30
00:01:29.220 --> 00:01:32.000
I'm gonna not repr,

31
00:01:32.000 --> 00:01:34.060
define an init method,

32
00:01:34.060 --> 00:01:36.960
and we're going to get a list of segments.

33
00:01:37.820 --> 00:01:42.500
So this is going to be a list of segment.

34
00:01:42.500 --> 00:01:45.430
Okay, we can import this, of course,

35
00:01:47.210 --> 00:01:49.660
and just import it,

36
00:01:49.660 --> 00:01:53.310
in Python you can press Alt + Enter, if you're on Mac,

37
00:01:53.310 --> 00:01:55.550
or I think it's Control + Enter if you're on Windows

38
00:01:55.550 --> 00:01:57.720
to get that, the type hints,

39
00:01:57.720 --> 00:02:01.270
and then we're just going to say self.segments

40
00:02:01.270 --> 00:02:02.410
equals segments.

41
00:02:03.270 --> 00:02:06.820
Down here we're going to define the class segment,

42
00:02:06.820 --> 00:02:09.730
which is just going to have a departure

43
00:02:09.730 --> 00:02:10.800
and a destination.

44
00:02:19.310 --> 00:02:21.230
Okay, pretty simple class there.

45
00:02:21.230 --> 00:02:24.190
All it does is it has a departure, for example,

46
00:02:24.190 --> 00:02:27.640
GLA, if I can type,

47
00:02:28.570 --> 00:02:31.730
and a destination, let's say London Heathrow,

48
00:02:31.730 --> 00:02:35.560
so that just gets stored in this segment object,

49
00:02:35.560 --> 00:02:38.520
and we're going to get a list of segments passed

50
00:02:38.520 --> 00:02:40.690
over to our flight constructor.

51
00:02:41.730 --> 00:02:45.850
Then, we're going to have something like a property,

52
00:02:45.850 --> 00:02:49.620
that is gonna be a departure point.

53
00:02:51.380 --> 00:02:54.440
So where does this flight begin?

54
00:02:54.440 --> 00:02:56.580
That, it begins

55
00:02:56.580 --> 00:02:59.830
in the first airport of the first segment,

56
00:02:59.830 --> 00:03:01.720
so that's where this flight is gonna begin.

57
00:03:01.720 --> 00:03:06.720
So we're gonna return self.segments[0].departure.

58
00:03:08.310 --> 00:03:10.200
Okay, and it's gonna return a string.

59
00:03:12.380 --> 00:03:14.540
So this is the departure point.

60
00:03:15.440 --> 00:03:20.440
Now if we wanted to overwrite the departure point,

61
00:03:21.860 --> 00:03:24.890
let's say, make the flight depart from

62
00:03:24.890 --> 00:03:26.580
Edinburgh instead of Glasgow,

63
00:03:27.810 --> 00:03:30.480
there is no easy way to do that,

64
00:03:30.480 --> 00:03:34.660
so I'm going to down here write some code to do that.

65
00:03:34.660 --> 00:03:39.660
Okay, I'm gonna say my flight is a flight object there,

66
00:03:39.820 --> 00:03:42.470
that has, let's say a single segment

67
00:03:42.470 --> 00:03:44.690
that goes from Glasgow to London Heathrow, okay,

68
00:03:44.690 --> 00:03:47.420
so now we have a flight object

69
00:03:47.420 --> 00:03:49.010
that has a list of segments,

70
00:03:49.010 --> 00:03:51.310
but it only has one segment in there,

71
00:03:51.310 --> 00:03:55.170
and the segment is going from Glasgow to London Heathrow.

72
00:03:56.290 --> 00:03:58.950
If you print the flight departure point,

73
00:03:58.950 --> 00:04:00.260
notice how it's a property,

74
00:04:00.260 --> 00:04:03.290
so you don't need the method brackets,

75
00:04:03.290 --> 00:04:06.690
you'll get Glasgow, or GLA for Glasgow Airport.

76
00:04:07.610 --> 00:04:11.310
If you then want to make the flight depart from Edinburgh,

77
00:04:11.310 --> 00:04:13.493
you would have to do something like

78
00:04:13.493 --> 00:04:17.800
flight.segments[0].departure equal Edinburgh.

79
00:04:20.460 --> 00:04:22.440
Not terribly simple.

80
00:04:22.440 --> 00:04:27.440
I would rather do flight.departure_point equal Edinburgh.

81
00:04:30.210 --> 00:04:33.680
But of course we know departure point is a method,

82
00:04:33.680 --> 00:04:37.152
so this, can it be done?

83
00:04:37.152 --> 00:04:38.710
Can you do this?

84
00:04:39.770 --> 00:04:41.180
The answer is no, by the way,

85
00:04:41.180 --> 00:04:43.530
you cannot do this right now,

86
00:04:43.530 --> 00:04:46.700
but of course Python knows that you may want to do this,

87
00:04:47.590 --> 00:04:50.980
and it provides you with another decorator you can use.

88
00:04:50.980 --> 00:04:54.780
The decorator is actually quite a magical thing now,

89
00:04:54.780 --> 00:04:58.340
because the decorator will be called departure_point.

90
00:04:59.670 --> 00:05:02.330
So now we use @departure_point which is the name

91
00:05:02.330 --> 00:05:05.980
of our property, .setter,

92
00:05:07.440 --> 00:05:12.440
and then, we can call our method the same thing,

93
00:05:14.120 --> 00:05:16.470
if you want, or you can call it something else,

94
00:05:18.315 --> 00:05:22.140
and of course it needs the value that you're going to

95
00:05:23.860 --> 00:05:27.080
pass in, that's the EDI down there,

96
00:05:27.080 --> 00:05:32.080
and then all we have to say is self.segments[0].departure

97
00:05:32.400 --> 00:05:35.610
equal EDI, okay?

98
00:05:37.360 --> 00:05:39.540
An even better way of doing things,

99
00:05:39.540 --> 00:05:42.710
just because of, sorry, the,

100
00:05:43.920 --> 00:05:45.900
the departure should be equal to val, of course,

101
00:05:45.900 --> 00:05:47.390
not EDI.

102
00:05:47.390 --> 00:05:49.950
An even better way of doing things is the following.

103
00:05:49.950 --> 00:05:53.850
Normally segments are specific flights,

104
00:05:53.850 --> 00:05:55.710
they have specific flight codes,

105
00:05:55.710 --> 00:05:58.350
specific numbers associated with them,

106
00:05:58.350 --> 00:06:00.360
so you can't just say

107
00:06:00.360 --> 00:06:02.970
this flight is now going to depart from Edinburgh.

108
00:06:02.970 --> 00:06:05.340
But what you can do is you can put a different segment

109
00:06:05.340 --> 00:06:07.940
in there altogether, if you want.

110
00:06:07.940 --> 00:06:10.540
So I would rewrite this, so instead of doing this,

111
00:06:11.750 --> 00:06:13.650
just in a professional environment

112
00:06:13.650 --> 00:06:15.770
where your segments cannot change

113
00:06:15.770 --> 00:06:17.660
because they have specific flight codes

114
00:06:17.660 --> 00:06:19.430
associated with them,

115
00:06:19.430 --> 00:06:24.430
I would say the destination is self.segments[0].destination

116
00:06:24.670 --> 00:06:29.590
and then I would say self.segments[0] is a new segment,

117
00:06:29.590 --> 00:06:31.340
where the departure is val,

118
00:06:31.340 --> 00:06:33.730
and the destination is dest,

119
00:06:33.730 --> 00:06:35.760
this thing we calculated up here.

120
00:06:35.760 --> 00:06:38.690
Potentially here you could also, you know,

121
00:06:38.690 --> 00:06:41.890
find out what the flight code is gonna be,

122
00:06:41.890 --> 00:06:45.120
find it in from a database or some other place,

123
00:06:45.120 --> 00:06:47.550
and you know, decide that there.

124
00:06:48.560 --> 00:06:49.660
The only reason I'm doing this

125
00:06:49.660 --> 00:06:50.800
is to show you again

126
00:06:50.800 --> 00:06:53.070
what you could do if you were not able

127
00:06:53.070 --> 00:06:55.060
to change the segments themselves.

128
00:06:55.060 --> 00:06:56.660
You could just create a new one,

129
00:06:56.660 --> 00:06:58.660
replace segments[0] for that,

130
00:06:58.660 --> 00:06:59.610
that would be fine.

131
00:07:01.170 --> 00:07:02.420
Okay?

132
00:07:02.420 --> 00:07:03.790
Now, you can do this.

133
00:07:05.130 --> 00:07:09.260
So you can delete flight.segments[0].departure equal EDI,

134
00:07:09.260 --> 00:07:11.790
and now say flight.departure_point equal EDI.

135
00:07:13.110 --> 00:07:16.220
And if you print flight.departure_point

136
00:07:16.220 --> 00:07:20.690
you'll see when we run flight.py, oh,

137
00:07:23.912 --> 00:07:28.000
yes, sorry, my bad, with the typing,

138
00:07:28.000 --> 00:07:31.670
you have to define this class before the other one,

139
00:07:31.670 --> 00:07:34.250
just the way that Python sees the classes,

140
00:07:34.250 --> 00:07:37.420
it evaluates this method typing,

141
00:07:37.420 --> 00:07:39.180
before it has imported the class,

142
00:07:39.180 --> 00:07:41.040
so you have to define that there,

143
00:07:41.040 --> 00:07:44.380
and then you can see that you can get Glasgow and Edinburgh.

144
00:07:44.380 --> 00:07:48.100
In my notes I actually don't have the typing defined

145
00:07:48.100 --> 00:07:49.050
in the methods,

146
00:07:49.050 --> 00:07:51.470
that's why I didn't realise this before.

147
00:07:52.720 --> 00:07:54.090
So as you can see,

148
00:07:54.090 --> 00:07:58.750
this helps you identify how to change the departure point.

149
00:07:58.750 --> 00:08:00.450
Now you don't even have to know

150
00:08:00.450 --> 00:08:02.400
when you're using the flight object

151
00:08:02.400 --> 00:08:05.030
that this is a property or not.

152
00:08:05.030 --> 00:08:08.150
So whether this is an @property decorated method,

153
00:08:08.150 --> 00:08:10.230
or whether it's an actual property of the flight.

154
00:08:10.230 --> 00:08:13.050
This lets you set them and get them.

155
00:08:15.350 --> 00:08:17.500
Just for completeness as well,

156
00:08:19.290 --> 00:08:23.621
I'm going to define a repr method.

157
00:08:23.621 --> 00:08:26.340
In order to show you how I would do this,

158
00:08:26.340 --> 00:08:28.480
this repr method has to return something,

159
00:08:29.430 --> 00:08:31.010
string in the format

160
00:08:31.870 --> 00:08:36.870
of Glasgow, London Heathrow, Toronto, for example.

161
00:08:39.280 --> 00:08:40.720
So how are you going to do this?

162
00:08:40.720 --> 00:08:44.980
How would you create a string that has this format,

163
00:08:44.980 --> 00:08:47.210
knowing the segments?

164
00:08:47.210 --> 00:08:49.520
I'd encourage you to give this a go, by the way.

165
00:08:49.520 --> 00:08:51.770 line:15% 
Pause the video and give it a go, if you can.

166
00:08:55.050 --> 00:08:56.350 line:15% 
Hopefully you tried that.

167
00:08:56.350 --> 00:08:58.090
What I would do is say,

168
00:08:58.090 --> 00:09:00.470
get a list of the stops.

169
00:09:00.470 --> 00:09:02.630
So the first stop of your flight,

170
00:09:02.630 --> 00:09:05.290
or rather where it starts is Glasgow.

171
00:09:05.290 --> 00:09:08.610
The second stop is the end of the first segment,

172
00:09:08.610 --> 00:09:10.510
so you've got the start of the first segment,

173
00:09:10.510 --> 00:09:12.380
end of the first segment.

174
00:09:12.380 --> 00:09:15.060
Then you have the end of the second segment,

175
00:09:15.060 --> 00:09:16.540
and then if it was going somewhere else,

176
00:09:16.540 --> 00:09:19.240
you'd have the end of the third segment, and so forth.

177
00:09:20.080 --> 00:09:22.040
So the stops initially are

178
00:09:23.420 --> 00:09:26.110
self.segments[0].departure,

179
00:09:27.150 --> 00:09:30.210
and self.segment[0].destination,

180
00:09:30.210 --> 00:09:32.710
these are the two initial stops that you know

181
00:09:32.710 --> 00:09:34.150
you're always going to have,

182
00:09:34.150 --> 00:09:36.020
you're always going to have

183
00:09:36.020 --> 00:09:37.550
segments[0].departure,

184
00:09:37.550 --> 00:09:39.360
and segments[0].destination.

185
00:09:40.680 --> 00:09:45.680
Then I would say for seg in self.segments[1] onwards,

186
00:09:47.480 --> 00:09:48.980
stops.append(seg.destination).

187
00:09:51.510 --> 00:09:52.440
Simple enough.

188
00:09:52.440 --> 00:09:55.650
You start then at the first segment,

189
00:09:55.650 --> 00:09:56.930
rather segment number one.

190
00:09:56.930 --> 00:09:59.290
The second segment, if there are any,

191
00:09:59.290 --> 00:10:03.523
and then you append the destination over to your stops,

192
00:10:03.523 --> 00:10:05.000
and at the end you can return

193
00:10:06.840 --> 00:10:07.673
.join(stops).

194
00:10:10.050 --> 00:10:11.410
I'm not sure if we've seen this before.

195
00:10:11.410 --> 00:10:14.040
All this allows you to do is

196
00:10:14.040 --> 00:10:17.810
join each of the elements of the stops list

197
00:10:17.810 --> 00:10:20.270
with this string.

198
00:10:20.270 --> 00:10:22.250
So it will essentially put the first stop

199
00:10:22.250 --> 00:10:23.730
at the start of the string,

200
00:10:23.730 --> 00:10:26.620
the second stop after the string,

201
00:10:26.620 --> 00:10:27.990
and then it will put another string after,

202
00:10:27.990 --> 00:10:30.262
and put the third stop, and so forth.

203
00:10:30.262 --> 00:10:32.960
So essentially creating a string that looks like this.

204
00:10:32.960 --> 00:10:35.160
Now we can just print the flight

205
00:10:35.160 --> 00:10:37.030
instead of printing departure point.

206
00:10:37.030 --> 00:10:38.580
We can run it again,

207
00:10:38.580 --> 00:10:40.170
and we can see that we've now

208
00:10:40.170 --> 00:10:41.830
gone from Glasgow to London Heathrow,

209
00:10:41.830 --> 00:10:43.870
over to Edinburgh to London Heathrow,

210
00:10:43.870 --> 00:10:46.410
and here we've learned about a few things

211
00:10:46.410 --> 00:10:48.250
like the setter,

212
00:10:51.159 --> 00:10:54.700
and also how to use the setter in a property,

213
00:10:54.700 --> 00:10:56.410
and we've got some more practise

214
00:10:56.410 --> 00:11:00.180
at defining some actual, real examples of code.

215
00:11:01.110 --> 00:11:01.943
That's it for this video.

216
00:11:01.943 --> 00:11:03.549
I wanted to show you

217
00:11:03.549 --> 00:11:05.120
how to use the setter in a property.

218
00:11:05.120 --> 00:11:06.458
So thank you for joining me,

219
00:11:06.458 --> 00:11:08.108
and I'll see you on the next one.

