Rock Solid Python with Python Typing Transcripts
Chapter: Typing in Python
Lecture: Representing Multiple Numerical Types
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Now, you might have caught a little issue here. Notice there's an error when I tried to use a floating point number. It says we got an int.
0:09
We expected an int and we got a float. Seems totally reasonable to use floating point numbers in a calculator, doesn't it? So what do you do?
0:20
Well, let's look over here. One option, I'll give you three options and we'll get to the best one last.
0:28
So first option is, sorry, you only add integers. I'm not a fan. Option number two is to say, well, instead of that, what we can do here is we can say
0:38
this is an int or a float, right? Union of int, float, or we have the simpler version in 3.10, right? And it can return any of those. Whew, okay.
0:51
Another option. I have four. is to say real number equals... Maybe make this a little explicit, int, float, right?
1:09
Maybe make it real number, how do you feel about this? And then down here we can do this as well,
1:16
put a real number, define this somewhere for ourselves. Can be more complicated than just two things, of course. And notice, now the error is gone.
1:24
If we say what type is this, it knows it's an int or a float. That is okay, and I'm actually kind of all right with that.
1:32
I guess that was another one of the options. I'll put this here so you have it as a progress through time.
1:42
So we could explicitly write in float everywhere, or we could define our interesting own type and then refer to it later. That's pretty excellent.
1:50
But finally, I will show you what we probably should do. If you go over to the PEP for type ints, they talk about this thing called the numeric tower.
2:00
So numbers in Python are pretty rich. There's a numbers module, which defines things like rational or integral numbers.
2:09
We have real numbers, we have complex numbers, we have integers, complex, but we've talked about float and int, we're just talking about.
2:19
And it says, rather than requiring you to go to the numbers and import a bunch of things here,
2:25
or coming up with what we've done so far where you pipe them all together,
2:30
it says, look, in the typing definitions, which is all we're talking about, right?
2:36
At least for now, when we get to the runtime stuff, this might make a difference.
2:39
But for this, it says, look, having float is what we're going to do when an arbitrary numerical type, arbitrary real number type is acceptable, right?
2:52
A decimal type or an integer representing, you know, a true integer, right? So you're just gonna use a float if you wanna be a float or an int.
3:00
It says, look, we realize that does not account for like fractions and rational numbers and some edge cases, but people are rarely working
3:10
with those and if you are, I guess union away if you like, right? Or just be real specific and say it only takes fractions.
3:17
Right, so the fix, even though we went through this interesting flow of like,
3:22
oh, we get to find this thing and it's a union and it's an int and a float and it could even be an int float complex.
3:28
So the fix is just to say float, float, float. And back over here, you can see integers accepted as well as that, right?
3:38
If you see what it takes, just says float, float, run it again. Sure enough, of course it works. The other thing to note over here is it says,
3:47
if you specify something as having the type float, an argument of int is acceptable. Similarly, if it's annotated as having a complex number,
3:56
then float and int are both acceptable. So if it really could take complex numbers, or it could take floats, or it could take ints,
4:04
then complex is what you want. If you don't wanna work in complex number land, which I don't blame you,
4:09
float is probably the most common reasonable answer here.