Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Understanding namedtuple Class Utilities

00:00 In the previous lesson, I showed you the lesser-used optional arguments to the named tuple factory. In this lesson, I’m going to show you some of the lesser-used utilities that are included in your constructed named tuple classes.

00:13 Every named tuple class that you create automatically has some utility methods and attributes attached to it. To avoid some clashes with your fields, these utilities all begin with an underscore.

00:24 There are both methods and attributes on the class. The methods allow you to construct new named tuple instances from the class using an iterable for the field values, generate a dictionary from your named tuple instance, and generate a new named tuple with a change in one of the fields.

00:43 This is to replace the fact that you can’t edit the tuple, as it’s immutable. There are also two attributes on the class. The first contains the name of the fields, and the second contains the value of any defaults.

00:57 Let’s go see these in the REPL. I’m going to start by showing you an alternate way of constructing your instances. The ._make() class method.

01:20 Now that I have a person, I can call ._make(), passing in an iterable of the values I want in my instance …

01:32 and there you go. Jane. For my American friends, the height is in meters. That’s just under five foot three. Let me stick this in a variable …

01:48 and with that variable, I can use the ._asdict() method to get a dictionary. Each field in the named tuple is used as a key, with each value as the value.

02:00 Pretty straightforward. Since tuples are immutable, you can’t edit the fields. To help with this restriction, namedtuple provides the ._replace() method.

02:11 Before I show you a replacement, note the ID of the p variable. This is a unique number that the interpreter assigns to an object. In a moment, when I overwrite p, you’ll see a new value here.

02:24 Let me call ._replace(). Note that this returns an object. The value of p is unchanged. The new value has an age of 84 instead of 83, because that’s what I called ._replace() with. Now I’m going to actually replace p because I reassigned it. By calling the id() again, you can see that it’s a different object. This isn’t the original one, but a new one. In addition to these three methods, there are also two class attributes. The first contains the name of the fields and is appropriately called ._fields.

03:08 The second contains the default values. Let me just re-create my developer from the earlier lesson.

03:27 Here are the developer’s fields and the developer’s default values.

03:36 The default values are found in an attribute called ._field_defaults. This is stored as a dictionary.

03:48 The attribute is empty if there are no default values, like in this case with the Person named tuple. In the next lesson, I’ll be talking about the Pythonic coding approach and how named tuples can help make your code more readable.


thanks for the nice tutorial. I can think of a few application of named tuples for my projects.

I often encounter the situation you talked about: reading data from csv files where I do not have control of the column names, which contain lots of invalid field names for named tuples. rename = True is a nice way around, but i cannot use my original column names for indexing the named tuple. I can think of a mapping though if the _field attribute preserves the order of the arguments given when creating the named tuple class. Therefore my question: is the order preserved?

Many thanks, Ben

Christopher Trudeau RP Team on March 10, 2023

Hi Ben,

I’m not 100% sure I follow your question, but I’ll take a stab. The named tuple is a tuple so the attribute order is important and preserved. You can use square-brackets to reference an attribute just like with a tuple.

The ._fields attribute is able to be used to create new named tuple instances, so it also must be in the same order as the attribute declaration of the class.

I think, that’s what you were asking. If not, hit me back. …ct

Become a Member to join the conversation.