Modern APIs with FastAPI and Python Transcripts
Chapter: Modern language foundations
Lecture: Concept: Type hints
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Now that you saw how useful type annotations can be, or type hits, let's just review the concept of them really quick.
0:06
So here we have a function with no type information. We have some running max. It's set to nothing in the beginning. What's it supposed to be set to?
0:13
I don't know some larger thing, that could be the largest order, that could be the value of the largest order,
0:20
who knows, right? There's no information communicated there. Also, here's a counter function. It takes something. Based on the name being plural,
0:27
I'm guessing I could loop over those things, but I'm not 100% sure. And then there is a total, probably know what to
0:32
do with that, right? So this is not super helpful. Adding types lights up a lot of insight into how it's meant to be used.
0:39
So now we have a optional int. So it starts out as nothing, but it's supposed to be set to an integer. And here we have a counter.
0:46
It takes terrible things so I could loop over it. And what's in them? It's an item. Because it's an item,
0:52
the editors know that it has a value and a name and it says "we return
0:56
an integer". So just reading this code without doing anything else or seeing how it's
0:59
used, we know much more about what's possible as well as our editors know what we should be able to do. So if we try to, add a,
1:07
you know, some kind of field lookup or something that doesn't exist, while we're looping over the items, it will be able to help us and say,
1:12
"No, you're doing that wrong. Its name or value, those are your only two choices". Cool, right?