Python 3, an Illustrated Tour Transcripts
Chapter: Type Annotations
Lecture: Introducing Annotations

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

Talk Python's Mastodon Michael Kennedy's Mastodon