00:10 Everything you can do with the base data type can be done with the named variant. In addition to that, a named tuple treats its members as attributes, giving you the ability to name the parts of the tuple and access them using dot notation.
00:34 The factory takes two required arguments. The first is a string giving the factory the name the created class will use, and the second is a specifier for the names of the attributes in the tuple.
00:46 This argument has several different formats. You can give it a string where each field name is separated by a space, a string where each field name is separated by commas, or any kind of iterable where each item in the iterable is the name of a field. For example, you can give it a tuple or a list with the attribute names inside of it.
01:07 Let’s go play with named tuples in the REPL. Consider a regular old tuple. Here I’ve created a tuple that represents a point in a grid where the X value is 2, and the Y value is 4. When I show the contents of the tuple, I see the 2 and 4 in parentheses.
I can access the pieces of the tuple using index notation. With the square brackets here, I’ve accessed the first item in the tuple. Like all iterables in Python, tuples are zero indexed, so the first position is denoted as
0. Tuples are immutable.
The first argument tells the factory that the underlying class will be named capital-P
Point. The second argument tells the factory that I want two attributes in the class, the first named
.x and the second
.y. Note that I’ve named the class using a capital letter to be consistent with Python style guidelines about class declaration.
If I examine the class in the REPL, it shows me that this is a class in the
__main__ module with the name
Point. With the class in place, I can create an instance object the same way I would with any other class.
The result of the
type() function is the same as the class a few lines above. It is still a tuple though. The whole point of a named tuple is you can access the attributes of the tuple using the names in the factory.
Like I said, it’s still a tuple, and like a tuple, it is immutable. Instead of a
TypeError, you get an
AttributeError, but either way, setting a value isn’t allowed. There’s a subtle thing here.
04:41 I should say name at least one more time in that sentence. This attribute is immutable. I’m not allowed to change it. John is John. If I want Bob, I need a new tuple. Likewise if I want to change the kids.
That means I can do things to it. As long as I don’t attempt to change what
.kids is referencing, I’m good. John can have more kids. In fact, anything you do to a list you can do to John’s kids, including removing all the items.
You just can’t point the
.kids attribute at a new list. This can be a bit confusing in practice. I generally try to avoid it when I’m coding. Although it is legal, it feels counterintuitive to me and might be surprising to someone who is new to named tuples. Before moving on, let’s spend a bit more time with the factory and learn the different ways you can specify the attributes of a named tuple.
06:41 you can do that instead. This variant can be handy if you’re processing CSV files whose first line is the column names in the data. I’ll be playing with CSV files in a later lesson in the course.
Iterating on a string returns the letters, so I get two fields, one named
x and the other
y. This is the least clear of the variations, but it shows that if you can provide an iterable of attribute names, you can create a named tuple.
You can’t name an attribute anything starting with an underscore (
_). This is a safety feature. There are utility methods and fields on the class that might cause name clashes, so to keep the attributes distinct from the utilities, all the utilities begin with an underscore.
I’ve created a dictionary here with the keys being the names of the arguments to
Point and the values being my data. I can then use the
**kwargs mechanism to create an instance based on the dictionary arguments.
I’ve shown you how to create
namedtuple classes using the factory found in
collections. The factory has optional arguments that gave you even more control over your class. In the next lesson, I’ll show you those.
Become a Member to join the conversation.