Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: namedtuples, better than plain tuples
0:00 Now, there's a problem with these tuples that we're returning here and let's go back
0:04 and actually put this into a variable for a minute
0:06 as we saw say this t tuple is this.
0:09 Now when we go to it,
0:11 notice it knows it's a tuple because you can count how many things are in
0:14 it and so on. And the way you access it is kind of like a
0:16 list or something. So you could say,
0:17 "Give me the country would be that, the state would be this, and the city would
0:22 be that". Well, that's not at all obvious that it's city, state,
0:26 country. What if it was city,
0:27 country, state? Because state is optional.
0:30 There's no communication here about helping us with this,
0:33 Okay? So this is a real big drawback of these tuples and passing them
0:36 back like so. They don't know what goes where.
0:39 So Python has a couple of mechanisms, or a couple of data structures, that make this
0:43 way, way better. We could use classes which require a lot of stuff for
0:47 us to talk about. But if you're looking for just a short,
0:50 sweet little thing like this, what we can do over here, is we go over here and
0:53 say "import collections" and then we can define a special tuple that gives it extra
1:00 properties. So I can say a "location = collections.namedtuple()"
1:05 and notice the first thing we put us the type name,
1:08 which should be the same as a variable name,
1:10 right? So this is what we program against,
1:12 and this is what it thinks its own name is,
1:14 so they should be the same.
1:16 And then here we just put what we think it has:
1:18 city, state, country, and the order here is the order
1:22 you specify them. So watch this.
1:24 We can go over here and use this type down here. Instead of passing back a
1:28 regular tuple, we would say do the same thing,
1:32 but notice the location is gonna take a city, state, country, so we could do like
1:36 this. There's the location, and I wanna leave this here for you
1:40 so you have it. But let's say, we're going to return a
1:44 location like that, and advantage just to tie it back to this example up here,
1:49 now we say "t2.", look, country, state, city.
1:53 It knows the things that it has,
1:55 doesn't matter what order they're specified in the tuple, you address them by name,
1:59 not by order, which is way,
2:00 way better. So I'll just put "t2.city" and comment those out
2:05 so you have it left in code there.
2:07 Alright, so when this comes back,
2:08 we could still do this trick of unpacking it,
2:11 but let's just go down here and say location again,
2:16 and now we can print out location,
2:18 which is gonna be looking nicer as well.
2:22 See, it's location, the city is
2:23 Portland, state is Oregon, country is USA.
2:26 So these named tuples make it much easier for us to come back here and program
2:30 against. We know it has a city, country, and state.
2:32 It doesn't matter the order and so on.
2:33 So, really, if you need to pass multiple things back from a function,
2:37 tuples and tuple unpacking is cool, but named tuples are probably what you actually want to work with.