Rock Solid Python with Python Typing Transcripts
Chapter: Typing in Python
Lecture: Functions: Basic Typing
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Next up, let's talk about typing and functions. You saw actually in the pep, the very first hint of type hints in Python
0:10
was around a limited subset of syntax for functions. And of course, there's really great things we can do here.
0:17
And they're really actually super important. Functions are ways to break complex code into a little box, a little Lego piece
0:25
that you can use in your other functions in your overall application. And having that Lego piece describe what goes in with types
0:33
and what comes out with types is incredibly helpful for conveying how the pieces fit together as you put them together in your application.
0:42
So what we wanna do here is we're gonna start with just something real simple. Now I'll just print typing functions like that.
0:49
What we're gonna do is we're gonna create a couple of different types of functions that we could use.
0:54
So let's start by creating one called fib or Fibonacci. And at first we're not going to do any typing on it.
1:03
Let's just write it out real quick, then we'll add types. So the Fibonacci algorithm in Python is really, really concise
1:09
because you have tuple unpacking and creation on one line. That's cool. So we can say next, say current and next.
1:17
The full spelled out next is a keyword built in. So let's just do like this. It'll be zero and one. And then we just say for nothing in a range
1:28
from range of n, we'll do current next is equal to next and then next plus current.
1:35
That's that awesome tuple creation and unpacking, tuple creation and unpacking. Then we'll just return current.
1:44
And let's go over here and say n1 equals fib of 1, 2, 2, and 3. 3 and we'll print out in 1 and 2 and 3
1:57
And you can see we get 1 1 2 I guess do one more just to show So pretty sure that we're getting the right thing which
2:09
We are not because I didn't put the 4 there alright 1 1 2 3 5 8 13 etc
2:16
So that looks like it's working great, but when I go to call it when I say hey, what about this fib? What does it take? It takes an n.
2:26
And what does it return? I don't know. You can actually see that PyCharm has done some work to say it takes whatever, but we're
2:37
sure that it returns an integer because the way the algorithm works, they were able to
2:41
trace through and say either of these are always integers and they always become integers and the thing you're returning is an integer.
2:48
So the editor is helping, but you shouldn't rely on the editor's guess. You want to be more explicit than that. So we can be more explicit and say,
2:56
instead of it saying it takes an any, right, when you saw here, fib in colon any, we can say, no, no, no, that has to be an integer.
3:04
So if somebody calls fib of seven, get the error, no, no, no, not a string, a number, not any number, an integer.
3:13
And then we can also express the return type as an int as well. So this is our first Fibonacci.
3:20
Let's suppose we want to protect against somebody possibly calling this Fibonacci with a small number,
3:28
you know, like a negative one. What is the Fibonacci negative one? We could raise an exception and say it has to be natural number 1, 2, 3, etc.
3:38
That's possible. Or maybe we want to just return an optional integer.
3:42
And say if you give us the wrong one, we'll test for small, just give it a different function name. So we have a slightly different version here.
3:50
I'm gonna pass in an int. And remember, when you say we could return an integer or possibly nothing, what we have to do is say optional
4:01
and import that from typing of the type. So in this case, instead of raising exception, I'm gonna say return a negative,
4:08
I'm gonna return none if they give us a non-natural number, not one, two, three, and so on. We'll say if n is less than or equal to zero,
4:17
return none else return fib of n. Now of course you would just add this check to this one here, but just to skip re-implementing it.
4:28
So let's do a one, a two, and I'll put that as five. And now you can see we get none and five over here as you would expect.
4:42
So typing works pretty similar to variables. All the rules we've applied, you could pass in literal strings, you have optional, you
4:50
have unions, you have return types, they have to either be concrete or explicitly optional.
4:56
But when we come over here now, and we say dot, you can see exactly you have all the
5:01
integer operations there because the type system is telling PyCharm this function returns
5:07
an integer, so all the stuff you work with here should be treated as if it's an integer.