WEBVTT

1
00:00:00.600 --> 00:00:02.060
<v ->Hi guys and welcome back.</v>

2
00:00:02.060 --> 00:00:04.700
In this video we're going to learn about type hinting.

3
00:00:04.700 --> 00:00:06.981
Type hinting is a new feature in Python 3.5.

4
00:00:06.981 --> 00:00:09.398
So you must be using that Python version or later

5
00:00:09.398 --> 00:00:12.050
in order to be able to use this.

6
00:00:12.050 --> 00:00:15.120
Here we've got a function called list average.

7
00:00:15.120 --> 00:00:16.570
And that takes in a sequence

8
00:00:16.570 --> 00:00:19.340
and returns the sum of the sequence divided by the length.

9
00:00:19.340 --> 00:00:23.410
You can call this with pretty much anything you want.

10
00:00:23.410 --> 00:00:28.410
Right, you can do list a-v-g with one, two, three.

11
00:00:28.869 --> 00:00:31.350
Obviously this is not a sequence

12
00:00:31.350 --> 00:00:33.480
and it's certainly not a list.

13
00:00:33.480 --> 00:00:35.400
So maybe if you're calling this

14
00:00:35.400 --> 00:00:37.270
you'll realise that it doesn't make much sense.

15
00:00:37.270 --> 00:00:39.010
But you can call it.

16
00:00:39.010 --> 00:00:41.060
It will give you an error if you do that.

17
00:00:42.060 --> 00:00:43.120
As you can see,

18
00:00:43.120 --> 00:00:46.390
it tells you that you can't do sum of an integer

19
00:00:46.390 --> 00:00:48.180
or len of an integer.

20
00:00:48.180 --> 00:00:50.270
But while you're coding,

21
00:00:50.270 --> 00:00:51.840
there's nothing there to tell you

22
00:00:51.840 --> 00:00:53.970
that you might be making a mistake.

23
00:00:53.970 --> 00:00:55.331
So let's fix that.

24
00:00:55.331 --> 00:01:00.331
You can write inside the here, inside the brackets,

25
00:01:00.680 --> 00:01:02.630
colon list.

26
00:01:02.630 --> 00:01:04.110
And what this tells Python is that

27
00:01:04.110 --> 00:01:07.020
this thing should be a list.

28
00:01:07.020 --> 00:01:08.734
After the brackets you can type

29
00:01:08.734 --> 00:01:13.734
dash greater than, making a small arrow there, float.

30
00:01:14.100 --> 00:01:15.550
And what that tells Python is that

31
00:01:15.550 --> 00:01:18.683
this function is going to return a float.

32
00:01:19.900 --> 00:01:24.190
So this is how you do type hinting in Python.

33
00:01:24.190 --> 00:01:28.312
However let me say that using the list object as a hint here

34
00:01:28.312 --> 00:01:29.895
is usually not recommended.

35
00:01:29.895 --> 00:01:33.630
Usually you'll be doing from typing import list.

36
00:01:33.630 --> 00:01:35.688
And I know we haven't looked at importing

37
00:01:35.688 --> 00:01:37.000
in Python just yet.

38
00:01:37.000 --> 00:01:38.750
We're going to learn about that in the next video actually.

39
00:01:38.750 --> 00:01:40.850
But you'll be using that

40
00:01:40.850 --> 00:01:42.290
and then you'll be putting that there.

41
00:01:42.290 --> 00:01:44.810
That's the most common way of doing it.

42
00:01:44.810 --> 00:01:46.670
We're going to learn about import in the next video.

43
00:01:46.670 --> 00:01:50.820
So this tells you that the sequence is a list of elements.

44
00:01:50.820 --> 00:01:52.260
You don't know what elements.

45
00:01:52.260 --> 00:01:54.020
And it returns a float.

46
00:01:54.020 --> 00:01:56.200
Notice though that VSCode,

47
00:01:56.200 --> 00:01:57.440
which is the editor that I'm using now,

48
00:01:57.440 --> 00:01:59.810
doesn't tell me that I could be making a mistake.

49
00:01:59.810 --> 00:02:01.090
If you're using PyCharm

50
00:02:01.090 --> 00:02:04.210
or if you're running a programme called a linter,

51
00:02:04.210 --> 00:02:06.910
then that programme or PyCharm will tell you,

52
00:02:06.910 --> 00:02:08.775
hey you're passing in a number here,

53
00:02:08.775 --> 00:02:11.780
but the expectation is that you should pass a list.

54
00:02:11.780 --> 00:02:14.823
So that is why we do type hinting.

55
00:02:28.080 --> 00:02:29.420
You can type a hint anywhere.

56
00:02:29.420 --> 00:02:33.510
So for example, you can type hint your class book.

57
00:02:33.510 --> 00:02:37.441
You can say that the name should be a s-t-r for a string

58
00:02:37.441 --> 00:02:40.070
and the page count should be an int.

59
00:02:40.070 --> 00:02:41.870
You don't have to specify a return value

60
00:02:41.870 --> 00:02:44.159
because this function doesn't return anything.

61
00:02:44.159 --> 00:02:47.100
Here we've got our from typing import list again.

62
00:02:47.100 --> 00:02:49.913
You can also import Tuple, Set and there's a bunch more.

63
00:02:49.913 --> 00:02:52.650
And you can use it like that.

64
00:02:52.650 --> 00:02:55.952
If you wanted to define that the books parameter

65
00:02:55.952 --> 00:02:59.530
should be a list of book objects.

66
00:02:59.530 --> 00:03:03.700
Given that the class book exists of course.

67
00:03:03.700 --> 00:03:05.330
So this is how you can do that.

68
00:03:05.330 --> 00:03:08.040
Also notice that I have signalled a string being returned

69
00:03:08.040 --> 00:03:09.520
from the s-t-r function

70
00:03:09.520 --> 00:03:11.540
and again that's that being used there.

71
00:03:11.540 --> 00:03:13.700
So the key benefit of doing this sort of stuff

72
00:03:13.700 --> 00:03:17.120
is that you'll get told if you pass in the wrong thing.

73
00:03:17.120 --> 00:03:18.856
As we learn more about Python,

74
00:03:18.856 --> 00:03:22.270
then we'll be using these type hints more and more.

75
00:03:22.270 --> 00:03:24.100
So you'll see me using them every now and then

76
00:03:24.100 --> 00:03:25.900
in the next few videos.

77
00:03:25.900 --> 00:03:27.860
One more example, that we've already seen,

78
00:03:27.860 --> 00:03:31.350
our book that has the class methods for hardcover

79
00:03:31.350 --> 00:03:32.230
and paperback.

80
00:03:32.230 --> 00:03:33.800
Now we can type hint it.

81
00:03:33.800 --> 00:03:36.060
And make sure that when we are using this code

82
00:03:36.060 --> 00:03:38.961
we get told whether the name, book type and weight

83
00:03:38.961 --> 00:03:41.780
arguments that we're using to create a book

84
00:03:41.780 --> 00:03:43.230
are correct or not.

85
00:03:43.230 --> 00:03:45.450
So here we're saying that the name should be a string.

86
00:03:45.450 --> 00:03:46.882
Name, colon, string.

87
00:03:46.882 --> 00:03:48.940
Book type should be a string.

88
00:03:48.940 --> 00:03:50.746
And the weight should be an integer.

89
00:03:50.746 --> 00:03:53.105
The repr function returns a string.

90
00:03:53.105 --> 00:03:56.930
And the hardcover and paperback methods take in a string

91
00:03:56.930 --> 00:04:01.090
and an integer and return a book object.

92
00:04:01.090 --> 00:04:04.170
This is how you signal that these methods

93
00:04:04.170 --> 00:04:08.377
return an object of the same type that you're currently in.

94
00:04:08.377 --> 00:04:12.890
You can't do this because it will give you an error

95
00:04:12.890 --> 00:04:15.210
because this method here is created

96
00:04:15.210 --> 00:04:17.660
before the class has finished processing.

97
00:04:17.660 --> 00:04:20.075
So this is created while the class is being processed.

98
00:04:20.075 --> 00:04:22.470
So that's why you can't use that there.

99
00:04:22.470 --> 00:04:24.650
So in Python we put them inside quotes

100
00:04:24.650 --> 00:04:27.786
to signal that this method returns a book object

101
00:04:27.786 --> 00:04:30.930
which is the same type that we're currently in.

102
00:04:30.930 --> 00:04:33.470
If this returned a different class,

103
00:04:33.470 --> 00:04:36.827
then you would do that, say if it returned a bookshelf.

104
00:04:36.827 --> 00:04:38.840
Then you can totally do that.

105
00:04:38.840 --> 00:04:41.171
But if it's the same type that you are currently in.

106
00:04:41.171 --> 00:04:43.800
Then you have to signal it like that.

107
00:04:43.800 --> 00:04:45.420
All right, just a few examples of

108
00:04:45.420 --> 00:04:47.858
how to use type hinting in Python.

109
00:04:47.858 --> 00:04:49.910
Again this is a refresher course,

110
00:04:49.910 --> 00:04:53.040
so we're not going into detail of how everything is working.

111
00:04:53.040 --> 00:04:54.467
But we will be using these type hints

112
00:04:54.467 --> 00:04:56.020
over the next few videos

113
00:04:56.020 --> 00:04:57.330
and you'll get more familiar with them.

114
00:04:57.330 --> 00:04:58.280
They're really straight forward.

115
00:04:58.280 --> 00:05:00.100
It's just there to help you as a developer.

116
00:05:00.100 --> 00:05:02.060
They don't stop your code from running.

117
00:05:02.060 --> 00:05:04.160
They don't raise any errors or anything like that.

118
00:05:04.160 --> 00:05:05.080
It's just to help you.

119
00:05:05.080 --> 00:05:06.100
Thanks for joining me in this video.

120
00:05:06.100 --> 00:05:07.927
And I'll see you in the next one.

