Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: namedtuples, better than plain tuples

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now, there's a problem with these tuples that we're returning here and let's go back and actually put this into a variable for a minute
0:07 as we saw say this t tuple is this. Now when we go to it, notice it knows it's a tuple because you can count how many things are in
0:15 it and so on. And the way you access it is kind of like a list or something. So you could say,
0:18 "Give me the country would be that, the state would be this, and the city would be that". Well, that's not at all obvious that it's city, state,
0:27 country. What if it was city, country, state? Because state is optional. There's no communication here about helping us with this,
0:34 Okay? So this is a real big drawback of these tuples and passing them back like so. They don't know what goes where.
0:40 So Python has a couple of mechanisms, or a couple of data structures, that make this
0:44 way, way better. We could use classes which require a lot of stuff for us to talk about. But if you're looking for just a short,
0:51 sweet little thing like this, what we can do over here, is we go over here and
0:54 say "import collections" and then we can define a special tuple that gives it extra properties. So I can say a "location = collections.namedtuple()"
1:06 and notice the first thing we put us the type name, which should be the same as a variable name, right? So this is what we program against,
1:13 and this is what it thinks its own name is, so they should be the same. And then here we just put what we think it has:
1:19 city, state, country, and the order here is the order you specify them. So watch this.
1:25 We can go over here and use this type down here. Instead of passing back a regular tuple, we would say do the same thing,
1:33 but notice the location is gonna take a city, state, country, so we could do like this. There's the location, and I wanna leave this here for you
1:41 so you have it. But let's say, we're going to return a location like that, and advantage just to tie it back to this example up here,
1:50 now we say "t2.", look, country, state, city. It knows the things that it has,
1:56 doesn't matter what order they're specified in the tuple, you address them by name, not by order, which is way,
2:01 way better. So I'll just put "t2.city" and comment those out so you have it left in code there. Alright, so when this comes back,
2:09 we could still do this trick of unpacking it, but let's just go down here and say location again, and now we can print out location,
2:19 which is gonna be looking nicer as well. See, it's location, the city is Portland, state is Oregon, country is USA.
2:27 So these named tuples make it much easier for us to come back here and program against. We know it has a city, country, and state.
2:33 It doesn't matter the order and so on. So, really, if you need to pass multiple things back from a function,
2:38 tuples and tuple unpacking is cool, but named tuples are probably what you actually want to work with.


Talk Python's Mastodon Michael Kennedy's Mastodon