Python 3.11: A Guided Tour Through Code Transcripts
Chapter: Type Updates for 3.11
Lecture: Class Types Don't Work Within Classes So Well
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Here's an example of python typehints and action. We have this function user by id. That gets us a user given its user id.
0:09
And possibly returns its user. You can see the blue typehints, it says that the user id. Must be an integer. it can't be none. It can't be a string,
0:20
has to be an integer. And the return value is one of two things. It's either a user that was located by finding it from that Id.
0:29
Or possibly you could have asked for a user Id. Whether is none. Maybe we have 100 users.
0:34
And you asked for a user? One million. Well we got no answer for you there.
0:38
So you're going to get none. So optional of user or you could write user pipe
0:43
None, if you prefer, tells us that it's either going to be a user or it's going to be nothing And that's super helpful when we consume this function,
0:53
we can know exactly what to do with it. So these typehints, as I said in the opening are really, really great.
1:01
Sometimes though they're not as great as they could be. Due to the way that python creates classes and functions and all those things.
1:12
It doesn't use a compiler where it pulls out the types and then compiles it and then sort of re evaluates
1:18
things like some compiled languages does know it just goes from top to bottom and says I'm gonna execute this code
1:25
The execute class colon user means will begin finding a class called user. It's not done until you get all the way through that,
1:34
but it's going to start it and we'll hit the def init. Well, now we're going to define a function init.
1:40
The internals. We can get that later. But right now we're going to define a method that goes with that user. Then we get to this next one here def.
1:48
Same gonna take the self that's current object in another user. That other user is supposed to be also the user class we want to use
2:00
Typehints to say that here, look, this other user colon user. But I haven't read something's wrong, isn't it?
2:08
Red is not good. Not in this case. What's the problem? Well, we can't say the user class because we're in the middle of defining it.
2:17
But this step that we're trying to propose with type ins is actually saying we need to use this type that
2:23
presumably exists to say what types here. It's like, well, we don't have that yet. We're still building it.
2:28
Hang tight. I do think python should be upgraded to just say, look, we're going to put a little placeholder here and come back and fill that out.
2:36
But that's not how it works. No, this is how it works. It says crash. Crash. Crash. Name, error user not defined what gives.
2:44
I mean, look, I did exactly the same thing in that function. Get user by id and it runs just fine. I said there's an optional user,
2:52
but here, when you're inside of a class, you cannot use that class, the name of that class. You get this crash.
3:00
Well, python 3.10 and before had one fix, python types in 3.11 and above have a better fix. So, lets go and explore in code.