Write Pythonic Code Like a Seasoned Developer Transcripts
Lecture: Prefer named tuples
0:01 We've seen how central tuples are to many parts of Python,
0:03 let's look at a better version of tuples.
0:07 So over here I have a very simple program; we have two methods,
0:11 "main", which is going to print out some data
0:13 and it's going to get that data from this get_data_tricky version,
0:16 you probably wouldn't call your function tricky but you know,
0:20 just to highlight it for the course, right?
0:21 So you can see it returns a list of tuples, here we have some incrementing numbers,
0:27 so maybe this is an id, we have 3 values here in the middle,
0:33 so those must represent something important, we'll see, not totally sure what that is.
0:38 So we'll come over here, here I've got a little template
0:42 for printing out some stuff
0:43 and notice I've got some values just so it can run,
0:46 so here we've got 3 things back and I just put "ones" everywhere.
0:49 Suppose I want to print out the id, the rating and the position.
0:53 Now remember, this is not the function I am getting it from,
0:55 so maybe you don't have it handy easy to look at,
0:59 so we are going to come over here and say well,
1:00 I think that I remember the first one being this
1:04 and let's see the rating maybe that was next,
1:08 and the last two got maybe those represent the position
1:11 and we know that doesn't look right because these are like floating point
1:14 and that's like and integer, so maybe actually this is a 3, what was that, that was 1,
1:19 yeah, yeah, OK, so that's right, so this is what we wanted, 0, 3, 2, 1,
1:25 what if I wanted to add an item, a thing, another element to this tuple?
1:30 How easy this is going to be to maintain, to review,
1:34 to bring on new people, and so on,
1:37 this is not a good way to work, so let's talk about a better kind of tuple.
1:41 so let's import collections, and let's define something called a Rating.
1:47 Now this is like defining a class or something like that, like a custom type,
1:52 but we can do it in a very concise short way, using "collections.namedtuple"s.
1:58 So what you put here is the name, the type name,
2:01 and then you can put the fields separated by commas,
2:04 so maybe we want to call this id, rating, "x" and "y", something like that.
2:09 So now, give us some space, so PEP 8 is not mad about our spacing,
2:13 we can write a better version here,
2:19 instead of doing all this, let's have a rating,
2:23 just like the way you initialize a class, or something like that,
2:26 here we are going to initialize our rating and it takes an id
2:30 and a rating and then "x" and then "y".
2:32 Actually, it looks like I have that wrong, so let's put it like this,
2:35 or I would have to reorder my data but let's say it goes like this.
2:38 Now, let's comment out this tricky version, here we go,
2:41 the data from the better and we'll hide this,
2:43 it didn't even matter what order it comes in,
2:45 we don't care, we don't have to look at it,
2:47 so we are going to say now we are going to work with this better version of data,
2:50 I want to put the id first, so let's see what is that, "d." all right, "id".
2:55 Cool, and we are going to have "d.rating" I think was next, "d.x", "d.y",
3:02 see how much nicer that is?
3:04 And literally, that is all it takes, name the type,
3:07 state the basically the names of the positions,
3:11 and then when you allocate it, instead of just saying regular tuple like this,
3:14 you just allocate it as an instance of a class.
3:18 So let's run it, make sure I didn't pull any sneaky tricks, that it still works,
3:22 boom, id 1, 2, 3, rating looks right, position x y, great.
3:27 And, these named tuples, they have all the properties that you would expect,
3:33 so for example, I can come down here I could say
3:36 let's say we want the x y value so I could have, so here, like this,
3:42 I could say I want to unpack this tuple and I want to print x and y
3:47 so you should just see just the numbers next to each other alongside the other values.
3:52 There is the x y values we are unpacking.
3:54 So these are regular tuples, they do everything regular tuples do
3:57 but they are upgraded and have names, wonderful.
4:01 So to create one, we just import the collections module,
4:04 we create a collections.namedtuple, we give it a name,
4:07 we catch the instance of the class, generate it from name tuple here,
4:13 and when I use that every time when I allocate one of them.
4:16 We name the positions, this case we have temperature, latitude, longitude and quality,
4:21 then you can see we create one and we can access it
4:23 in the traditional style in bracket 0 but much better "m.temp", "m.quality".
4:28 We print it out, we even get a nicer looking string friendly version
4:32 rather than just the basic tuple without the names or understanding
4:37 of what the positions mean, so named tuples, very Pythonic,
4:41 definitely make use of them.