WEBVTT

1
00:00:00.000 --> 00:00:04.885
[MUSIC]

2
00:00:04.885 --> 00:00:08.000
Hey y'all, I'm Kenneth,
your Treehouse Python teacher.

3
00:00:08.000 --> 00:00:11.340
Today I want to talk about an interesting
newer feature in Python that has caused

4
00:00:11.340 --> 00:00:12.540
a bit of a ruckus.

5
00:00:12.540 --> 00:00:16.850
Python, like JavaScript and Ruby, and many
other languages, is dynamically typed.

6
00:00:16.850 --> 00:00:20.955
This means that a variable like name could
change from an int to a string to a list,

7
00:00:20.955 --> 00:00:22.770
to whatever else it needs to be.

8
00:00:22.770 --> 00:00:26.410
This is different from languages like
Java and C#, where a variable has a type

9
00:00:26.410 --> 00:00:30.340
declared when it's created and it will
always hold some data that fits that type.

10
00:00:30.340 --> 00:00:32.720
But you probably already know this,
so why am I telling you?

11
00:00:32.720 --> 00:00:34.620
Well, with the acceptance
of some recent PEPs,

12
00:00:34.620 --> 00:00:37.295
Python has an officially supported
way of doing type hinting.

13
00:00:38.580 --> 00:00:43.070
It started ten years ago when
PEP 3107 landed in Python 3.0.

14
00:00:43.070 --> 00:00:45.460
This PEP allowed for function annotations.

15
00:00:45.460 --> 00:00:49.177
You could provide information about
the parameters that a function took in

16
00:00:49.177 --> 00:00:50.298
its return value, and

17
00:00:50.298 --> 00:00:54.075
these would show up in the __annotations__
attribute of the function.

18
00:00:54.075 --> 00:00:57.743
Then came Mypy,
a static type checker for Python.

19
00:00:57.743 --> 00:01:00.822
This tool would look for special
formatting in your Python code where you'd

20
00:01:00.822 --> 00:01:02.650
declare what type a parameter should be,
and

21
00:01:02.650 --> 00:01:05.540
then it would tell you if you
ever gave it something else.

22
00:01:05.540 --> 00:01:07.620
This checker and
many others like it didn't and

23
00:01:07.620 --> 00:01:09.710
won't make Python statically typed.

24
00:01:09.710 --> 00:01:12.324
They didn't and
don't enforce anything at runtime.

25
00:01:12.324 --> 00:01:14.884
Instead they're used
during code reviews and

26
00:01:14.884 --> 00:01:17.844
in build processes to make
sure code fits a standard.

27
00:01:17.844 --> 00:01:21.974
In Python 3.5, Guido introduced PEP 484,
which officially laid out

28
00:01:21.974 --> 00:01:26.500
a specification for how these type
annotations should look and behave.

29
00:01:26.500 --> 00:01:29.205
This is also when the typing
module came into Python,

30
00:01:29.205 --> 00:01:30.985
which we'll look at more later.

31
00:01:30.985 --> 00:01:35.725
And for Python 3.6,
PEP 526 gave us variable annotations.

32
00:01:35.725 --> 00:01:39.145
So now you can annotate parameters
to functions and methods, and

33
00:01:39.145 --> 00:01:42.965
state what type of data you'd like
a particular variable to hold.

34
00:01:42.965 --> 00:01:44.625
These PEPs caused quite a stir.

35
00:01:44.625 --> 00:01:48.044
People jumped to conclusions and declared
that Python was abandoning its dynamic and

36
00:01:48.044 --> 00:01:49.075
duck type roots.

37
00:01:49.075 --> 00:01:51.270
Of course, that hasn't and won't happen.

38
00:01:51.270 --> 00:01:54.620
All of this type hinting is just another
tool to use when you're developing.

39
00:01:54.620 --> 00:01:57.000
It's especially useful for
larger code bases or

40
00:01:57.000 --> 00:01:59.640
code where you can't look through
all of the source easily.

41
00:01:59.640 --> 00:02:01.546
PyCharm supports these
annotations natively and

42
00:02:01.546 --> 00:02:03.650
will show you warnings
if you violate them.

43
00:02:03.650 --> 00:02:06.100
If you don't use PyCharm,
the Mypy tool will do the same.

44
00:02:07.130 --> 00:02:09.554
Like I said,
type hinting isn't going to change Python.

45
00:02:09.554 --> 00:02:14.034
It's 100% opt in per file, and
even per function or per variable.

46
00:02:14.034 --> 00:02:16.220
In fact even with the type hint and
a static checker,

47
00:02:16.220 --> 00:02:19.680
you can send any data you want to
any function or variable you want.

48
00:02:19.680 --> 00:02:21.970
Well, assuming it won't
cause an exception.

49
00:02:21.970 --> 00:02:22.820
Like doc strings,

50
00:02:22.820 --> 00:02:26.040
testing, and naming conventions,
it's a tool to make your work easier.

51
00:02:26.040 --> 00:02:28.510
It's something to discuss and
decide on with your team.

52
00:02:28.510 --> 00:02:31.020
If you decide to use it though, I'm sure
will save you more than one headache.

53
00:02:32.020 --> 00:02:33.742
So let me show you about type hinting.

54
00:02:33.742 --> 00:02:36.823
We'll start with a really straightforward
example, then talk about generics and

55
00:02:36.823 --> 00:02:38.319
optionals, and how to use stub files for

56
00:02:38.319 --> 00:02:40.090
compatibility with older
versions of Python.
