Rock Solid Python with Python Typing Transcripts
Chapter: Typing in Python
Lecture: Typing for Container Data Types

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Collections and data structures in general are really interesting in Python typing land.
0:06 So we're going to talk about a couple of ideas here around collections and how we express their types and the type of things they contain.
0:15 Now in Python, it's a super dynamic language. It has no explicit types traditionally.
0:22 So we can do things like smush heterogeneous types together into a given collection.
0:28 Let's create some types and play around with that idea just for a second. So we'll create this person here. And let's make a list of things.
0:46 Standard old simple list, things. And maybe the first thing that's going to go in there is that. We'll have a person.
0:52 Their name is Michael and their number is 42. Oh, I have those backwards. Let's swap those. I'm kind of wrong to say the number first.
1:00 Don't want to go to Iron Maiden, shout out to Iron Maiden. I'm not a number, I'm a free man. All right, so my goal number there, and let's see, 7.14.
1:10 We also have seven here like this. We have a sane, Carpe Diem, all those things, right? So there's nothing wrong with this in Python.
1:20 Oops, let's make sure we're running the right one. We run this, what do we get? A bunch of output. We got seven, we got some person object
1:31 that a memory address, 7.147 in another way as a string, seize the day, all that saying. We can have, you know, we could even have a list
1:40 within the list, right? Put our start of our Fibonacci numbers in there. There's nothing wrong with this in Python.
1:49 We can completely have these heterogeneous types just like we define a number and a name on the person type, But then later we could come along and go,
1:58 you know, person.ssn is 434, right? Let's just, instead of printing, let's just put just there.ssn in here. And even though there's a warning like,
2:12 hey, this doesn't exist on this type, we can do that, right? So Python is super dynamic in this way. We can take the types and change them at runtime.
2:20 We can, our collections can have lots of different things. Why do I bring this up? 'cause even though it's possible,
2:28 it's not really how we use our code, it's not how we write programs. Typically, when we define a class, the things we put in there,
2:37 that's what we want to be in there, we just use it. This, let's say, nah, probably not, okay? Similarly for the things we got here,
2:48 like yes, you can do this, but just 'cause you can doesn't mean you should. So let's look at the right way, you know, right as according to Michael,
3:01 but also the way I think I see people doing things all the time. Take that away so it doesn't crash 'cause commented out the addition of the SSN.
3:10 So how do people really use these things? Well, they actually have the same type of thing. If you want a bunch of numbers,
3:17 maybe we want just the prime, well, see my memory here, and so on, right? Maybe we want to start at the prime numbers here, right?
3:27 Everything in here is going to be the same. Similarly, if we want people, could be, put our Michael in there, but we could also put Sarah,
3:42 and you know, Sarah, she just loves the number three. It's amazing. And Zoe loves 100. Don't know why.
3:53 Right, so these things are homogeneous types of containers or when you define the type, they're set and fixed, right? So in the typing system,
4:04 you can't really do a lot about this, but also when you're programming, that means as you're interacting with the thing, sure, you can print them out,
4:10 but what do you really do with them, right? It's not a great way to work. Here, you know that these are not just a list, but a list of integers.
4:18 Here we know we have a list of not just a list, but people, person objects. So now we can go and set an explicit type on these.
4:25 We can say this is a list. Now, traditionally, you may have seen this. From typing import list, capital L, this is very important,
4:37 and then you would see capital L is for this. And then maybe, as we already saw, when you want to say what is in here, kind of like the callable,
4:45 we'll say this is a list of integers. This is totally fine, but this, I'll put this like so,
4:53 you have it. This is how it was when it was introduced. And everyone was like, this is
5:00 super annoying, I got to keep importing list. And we have a built in thing called list.
5:06 And that's actually what the type is, it's actually a list, not one of these, right.
5:10 But you weren't able to do this to it until I believe 3.9 or above. The typing system
5:17 has evolved in a lot of the recent releases of Python. And the collections is certainly
5:21 one of them where you have the capital L list, the old school way, and the new lowercase list. All right, same thing for here.
5:28 We have a list of this time person. That's our type that we defined above. And now when we work with numbers, hit dot, what do you see?
5:39 All the options, all the things that you can do for list because guess what? We know for sure it is a list.
5:46 But more importantly, if we go to an item of it and we say dot, we know that the things within it are guaranteed to be integers.
5:54 So I could call two bytes. I don't want to, but we get autocomplete for it to help us with that, right?
6:03 Similarly for people, list, of course, but we have a person object and the person has a name and a number out of its strong type, right?
6:14 So let's, we can just print these out, I suppose. So you see something interesting happening. You can see there's the bytes of whatever
6:26 that number was there, which is two. And there's the two. And then Michael is the name we printed out of the first item.
6:35 Okay, so this is how we do, how we work with collections. We say the collection type bracket, the contained type.
6:43 And again, this style of programming where you have a homogeneous, consistent style or type of thing contained in these collections is not just a recommendation.
6:55 It's the way that I see almost all code written. These are possible, but they're just they're kind of not really that useful.
7:03 And they also don't have a great representation other than we could say a list of any, right?
7:09 import that from typing, which means, of course, you can do things dot and get list operations, but on T, we have no idea what T is.
7:20 Good luck with that.


Talk Python's Mastodon Michael Kennedy's Mastodon