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

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's just look at the basic syntax that was introduced in Pep 3107.
0:03 Here we have a function foo and it's taking two parameters a and b
0:08 and following that we see a colon and then we see this expression here.
0:13 So this here is the type annotation here,
0:16 note that b is a default variable, it has value 5 for the default value
0:22 and in here we can put any expression, any Python expression in here
0:27 and we'll see some Python expressions.
0:29 Note that there's also support for *args and **arguments.
0:33 So you can just put a colon following them and put an expression in there
0:37 and there's also a way to specify the return value,
0:40 in order to specify the return value,
0:42 this introduce this little arrow operator dash greater than ->
0:45 and then we put the expression following that
0:48 and so if you have a function called sum
0:51 and you want to indicate what it's returning
0:53 you put that in this expression at the end,
0:55 note that it's coming before the colon there.
0:57 One thing to note is that annotations are not supported in lambda functions,
1:01 so if you're a super fan of lambda functions and use those all over the place,
1:05 you're not going to get the benefits of using annotations there
1:08 because you can't annotate them.
1:11 526 introduced the ability to annotate variables.
1:15 And so this is how we annotate variables,
1:17 here we've got a name variable and we just put a colon and this expression
1:21 in this case we're saying that this is str, a string
1:25 and note that this is the string class and that's a valid expression.
1:29 One thing to note about this pep is it introduced this construct down here,
1:34 which is a bare annotation on a variable with no assignment to it.
1:38 So here I'm saying there is a variable called name2
1:41 and it will be a type string, but I'm not giving it a value.
1:45 Note that if we simply said name2 by itself without the annotation
1:49 we would get a name error in Python.
1:51 But in this case, it's going to create an annotation for that variable,
1:54 note that this variable also does not exist at this point in time.
1:58 If we say name2 after it, we will get a name error.