Python 3.11: A Guided Tour Through Code Transcripts
Chapter: Type Updates for 3.11
Lecture: Self Type Demo

Login or purchase this course to watch this video and the rest of the course contents.
0:00 All right, here we are in Chapter six. Let's add type self. So we're gonna play with somehow referring to yourself within a class.
0:12 Now let me take the code I had from that presentation in the slides there and here you have it Sure
0:19 enough. PyCharm is warning us something is going wrong. Unresolved reference user. This has always been confusing to me because guess what?
0:27 I'm literally within the user class. How can you not possibly understand what the user is? However, if I try to run it sure enough,
0:39 name, user is not defined. Well that's unfortunate, Python 3.10 and before had this interesting way. If you put this in a string and you hover over
0:50 this it's kind of gives you this weird color of the string. And if we say U1 equals user. U2 equals user.
1:01 Let's give them some id's Two and one have to be we have U1.same And what goes in here notice it knows it's a user not a quote user but a real user
1:11 User. Say U2. And I can even print out whether they are the same like that, are they the same? No because they have different ideas.
1:23 But if for some reason we make their id's the same then yes. Yeah that's good enough for us.
1:28 Sure enough, they're the same. And so this is how you've done this previously and it's a bit of
1:34 a hack. Right? So we say it takes the user now notice again because and by the time line
1:42 seven finishes running the user class is defined. We could have that other function I wrote.
1:48 So here if we're going to get this let me just say user of user Id. Right? That's a bit of a hack.
1:54 But let's say that's what we're gonna do notice here, we have our user right there and code runs. No problem at all.
2:03 So it's only within the class here that this is a problem. So let me go and give this a slightly different name.
2:12 We'll just change the name for the next one. Let me call this 311 because this is gonna be the new feature, not the old one with the quotes.
2:23 So now what we can do instead of having the string hack, we can come up here and we can say from typing import self,
2:30 this is a Python 3.11 thing. And within a class we can say, look, I don't know what you are, you're not finished being defined yet.
2:40 But if you're up here, you called yourself user down here, you're going to call yourself user again. Alright. And by the way,
2:47 let's come back to the old one. Let me just show you that if I asked U1.same
2:52 as the number seven, it gives you a message and says no no no we expected seven. So we get expected a user and we got the number seven.
3:02 The same thing will happen down here and way of self, If I say U1.same as seven, what's it gonna say,
3:10 expected user matching generic type self now. And instead we got this end.
3:16 So a little bit more theoretical way of explaining what's going on here because the generic as well,
3:23 we're not seeing the type explicitly, it's inferred from the type that is spoken within in this case the user
3:31 class, but you can still see that that would give you the same error,
3:34 so same type of thing. But now you don't have to have this weird hack and for some reason you
3:38 change the name, there's not this string that's hanging around that no longer makes sense. A little bit more hide into the type system. Cool,right.


Talk Python's Mastodon Michael Kennedy's Mastodon