Python for .NET Developers Transcripts
Chapter: The Python Language
Lecture: Concept: Type annotations
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Using type annotations or type hints that kind of go by these two names in Python is super, super useful. The bigger your application gets
0:09
the more help you want your editor and other tooling to say, you know what? That is a Wizard or that is a string or that's an optional string
0:17
and you're not checking it for None and things like this. As of Python 3.4 we can have types in our Python code. So here, for example
0:26
we have wizard class and its constructor. It defines two fields, two public fields which are an optional string for the name
0:34
and an optional integer for the level. And they're both set to None. Using the Optional[str] Optional[int] defines those types.
0:44
Then our train method takes an integer base level and returns a Wizard. Because Python parses and defines these types all at the same time
0:53
it's not until the end of this whole code block that wizard is defined as a thing. So, we have to put wizard in quotes only within this class.
1:01
The rest of the application we just say wizard no quotes treat it like a regular type. There's this little edge case here.
1:07
The newer versions of Python are working around this have better syntax. But, you know, it's not a big deal
1:13
and I want to make sure this is like broadly applicable today and not just in the future. So, "Wizard" at the end here.