Rock Solid Python with Python Typing Transcripts
Chapter: Typing in Python
Lecture: Functions: Functions as Objects
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
The last thing I want to look at in this function section is what do you do to express when the function itself is a parameter.
0:11
Functions are what are called first class objects in Python. That means you can pass them around, you can create them as lambda expressions, inline,
0:19
all sorts of cool things. So let's suppose we want a function here called use function that receives a function.
0:25
So we'll say def use function, and it's going to get a function f and it has no type in the beginning, but it knows, it assumes it's a function.
0:37
Say something like what is your favorite number or whatever. And up here we can say use a function.
0:44
We need to give it a function, let's give it a lambda expression for p1, p2 for the parameters.
0:49
And then what's going to happen when you call it, passing Michael and 42 or whatever you
0:54
do, we're going to do a print statement for an f-strings, we'll say, he won. He to P ones
1:09
favorite number is P two, and then we're going to pass that function down to use function,
1:15
it'll be passes F, we call it, Michael will go to p 142, we'll go to p two, we'll go to
1:21
print statement. Let's give it a try. Michael's favorite number is 42. Well, that's really nice.
1:28
But do we know we're using it correctly? Like, let's see. What will pycharm tell us? Okay,
1:34
it takes a function. And it literally just says it takes f is it actually a function?
1:39
We don't know. Let's hover over here. It says it's an any and it returns none. Okay, well,
1:44
that really means nobody knows what to do with this, not even pycharm. So let's see if we could be more explicit about that.
1:58
What if we could describe the type of function that was allowed here? So in the typing namespace, we have what's called a callable.
2:06
Not just functions, but these could be classes with the dunder call. There's even, I think, a PEP out to make a module itself callable.
2:13
It doesn't really matter. It just means you can do this to it. You can execute it in some sense, right?
2:21
And not just can it be called, but what are its arguments and what are its return types.
2:26
For this, it's a little multi-layer, you put a list of the arguments and the arguments are going to be a string and an int.
2:34
And then you say the return type, which is none. So now if I come over here and I say f of, guess what, it takes a string and an int.
2:45
And if I go over here and I want to pass something, you see, well, it's going to require something. Let's define, let's just try a lambda function.
2:51
I'm not sure PyCharm will catch that. But let's say lambda of A goes to return A, or just A times A. Nope, it should be checking that.
3:03
Apparently it's not. Let's make some other functions. We'll just create them down here. We'll say def usable func one.
3:12
This is going to be, takes a name and a number and returns none. Make this correct for the moment, say int.
3:32
Robbed the implementation here, we'll say name's favorite number or call it common number in this case.
3:41
So you can tell there's some kind of difference. And now we can use func and pass usable func1, not calling it but passing it like that.
3:51
Let's try another one that's got a different signature. See if that'll get caught in this case. 2. And we'll just say ""Hello, name!
4:09
See if we can get it to complain if I get that wrong there. No, but if I run it, sure enough, you can see there is an error here.
4:17
So I guess our editor is not catching this one, but that's okay. Still helpful to have it around, right?
4:26
So if you really want to be explicit about saying, I take a function just like this, you say typing.callable of string comment,
4:36
those are arguments, string int, and I expect it to return nothing. And at a minimum, you can see it didn't catch the error
4:44
where we're using it wrong, but at a minimum, it tells you, well, unnamed variable string is the first thing, unnamed variable int
4:52
is the second thing you have to pass. We can probably get it to show us that if I had another number here
4:59
that know that number doesn't go there, yes, it does. And if this is 4.2, it'll say, The second thing must be an integer, not a float.
5:08
So that's really excellent. We get some help from knowing what the type is right here.
5:13
If we say probably x equals, well, it says all you're getting out of there is none.
5:18
So that's not going to be super helpful for you, but we don't quite get the same errors we saw before.
5:25
So typing not callable is how you express the signature of a function when you're actually
5:30
passing it as an argument or storing it as a variable for that matter.