Python for the .NET developer Transcripts
Chapter: The Python Language
Lecture: Concept: Type annotations
0:00 Using type annotations or type hints
0:02 that kind of go by these two names in Python
0:04 is super, super useful.
0:07 The bigger your application gets
0:08 the more help you want your editor
0:11 and other tooling to say, you know what?
0:12 That is a Wizard or that is a string
0:15 or that's an optional string
0:16 and you're not checking it for None and things like this.
0:19 As of Python 3.4
0:21 we can have types in our Python code.
0:24 So here, for example
0:25 we have wizard class and its constructor.
0:27 It defines two fields, two public fields
0:30 which are an optional string for the name
0:33 and an optional integer for the level.
0:35 And they're both set to None.
0:37 Using the Optional[str]
0:39 Optional[int] defines those types.
0:43 Then our train method takes an integer base level
0:46 and returns a Wizard.
0:48 Because Python parses and defines these types
0:51 all at the same time
0:52 it's not until the end of this whole code block
0:55 that wizard is defined as a thing.
0:57 So, we have to put wizard in quotes only within this class.
1:00 The rest of the application we just say wizard no quotes
1:03 treat it like a regular type.
1:05 There's this little edge case here.
1:06 The newer versions of Python are working around this
1:09 have better syntax.
1:10 But, you know, it's not a big deal
1:12 and I want to make sure this is like broadly applicable today
1:15 and not just in the future. So, "Wizard" at the end here.