Python 3, an illustrated tour Transcripts
Chapter: Type Annotations
Lecture: Introducing Annotations
0:01 In this video we're going to look at a new feature in Python 3
0:04 variable and function annotations.
0:07 There are a bunch of peps that talk about this, briefly discuss them.
0:11 The first pep 3107 was a pep that showed a suggested usage of function annotations
0:19 but was basically generic, it hinted at some of the purposes
0:22 of what annotation would be used for in the future
0:25 but was just thrown out there to test the waters
0:28 and see what other people are thinking about that.
0:31 A couple of other peps, 482 and 483
0:34 go over the literature, type, theory and what's out there
0:38 and then we come to 484 which had the first standard for Python.
0:43 526 updated that a little bit and gave us variable annotations
0:48 which didn't exist before that
0:51 and then there's a pep in the pipeline 544 that talks about structural subtyping.
0:56 In typing world there's a couple different types of type checking
1:01 one is called nominal type checking, another one is called structural type checking
1:05 and nominal basically says you've got this class
1:09 and I'm going to confirm that it's a class versus structural subtyping,
1:12 which one can think of as duck typing for subtyping
1:17 so I can say rather than this is a list of things that are getting passed in,
1:20 this is an iterable, that sort of thing.
1:23 Let's look at some of the motivation, pep 484 states
1:25 introduces a provisional module to provide syntax for function annotations and tools
1:30 along with some conventions for situations where annotations are not available.
1:35 We'll look at that a little bit more.
1:37 There's also 526, pep 526 states this pep aims at adding syntax to Python
1:42 for annotating the types of variables including class variables and instance variables
1:46 instead of expressing them through comments.
1:48 So pep 484 had some ways to express types through comments
1:51 and pep 526 provides a syntax for that.
1:55 So if you've been programming in Python,
1:57 you'll know that Python is a dynamically typed language.
2:00 You don't have to define what type your variables are and whatnot
2:04 and these annotation peps somewhat change that, we'll look at how they do it.
2:09 They allow us to document what the types are in our code.
2:13 And one of the things to know about this is
2:15 that these types that we document in our code have no effect at runtime
2:20 there are annotations, there are hints,
2:23 but Python the C Python interpreter that you download from python.org
2:27 is not going to run faster or slower because of them.
2:30 It's not going to interpret them at runtime and slow things down,
2:33 it's also not going to unbox things at run time and speed things up.
2:36 So it is neither faster nor slower.
2:40 There are two ways of commonly checking types
2:43 one is called static type checking and another is dynamic type checking
2:47 and this deals with when we check the types,
2:50 do we check them at compile time or runtime typically,
2:54 so a language like Java, when you compile your Java code
2:58 it's going to check the types and make sure that they're compliant
3:01 and typically the Python language will check types at runtime,
3:06 so when you're running your code, there is no real compile step in Python typically,
3:10 but some developers in the Python world came from Java or other typed languages
3:14 and they wanted the static typing benefits.
3:18 So what are some of the benefits of static typing?
3:20 One of them is when you get a large code base
3:23 unless it's extremely well documented and written in a clear manner,
3:27 it can be kind of hard to understand what the types are
3:30 that are coming in and out of function calls or constructors or method calls
3:36 and so annotation can aid comprehension for these sorts of code bases.
3:41 Another place where they're useful is they can catch bugs
3:43 and they can catch them early on
3:45 and if you read the literature about when you catch bugs,
3:48 the earlier you catch bugs the cheaper it's going to be
3:51 so you really want to push catching bugs sooner if you can,
3:54 ideally you don't want to write bugs or have them
3:56 but if you do have them and you can catch them earlier that's a lot cheaper
3:59 if you can have some sort of process that finds them right after you write your code
4:03 that's going to be cheaper than shipping your code
4:05 and having some end-user find your bug
4:08 and having to report that back, that sort of thing.
4:10 Another benefit is auto-completion.
4:13 Some of these newer editors such as PyCharm can take advantage of the annotations
4:16 and provide better auto-completion than you get otherwise
4:20 and also refactoring, they can allow you to refactor
4:22 because they know about sort of types you're expecting.
4:25 So pep 307 says that the aim of the annotation
4:28 is to provide a single standard way of specifying a functions parameters and return values
4:33 the use for annotation goes beyond just the inputs for functions and the outputs of functions.
4:38 You can mark the types for functions and classes and you can also mark variables.
4:43 Again, note that these annotations that Python supports in Python 3
4:48 don't actually do anything, when you run your Python code
4:51 the annotations are sitting there
4:53 but Python isn't going to check or do anything with them.
4:57 In order to do that, we need a third-party tool.
4:59 And so one of those, mypy is a tool that we'll look at how to use that.