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

Providing Optional Factory Arguments

00:00 In the previous lesson, I showed you how to create a named tuple class using the factory found in the collections module. In this lesson, I’ll introduce you to the optional arguments to the named tuple factory.

00:12 The named tuple factory function has some optional arguments that give you finer-grained control on your resulting class. With them, you can automatically rename attributes that have illegal names, specify default values for certain fields, and override the default module name of __main__.

00:30 Let’s check these out in the REPL. One use of a named tuple is to represent fields in a data record. When you do this, you might be reading that data and the names of the fields for that data from a third-party system. That might be a file or a database, but either way, it is something where you may not be in control of the field names.

00:52 You saw in the previous lesson that there are some restrictions on attribute names for a tuple. There’s a way around this in case you aren’t the one specifying your field names. I’ll start by importing the factory … and now I’m going to create a string with some illegal column names in it, simulating the field names coming from somewhere else.

01:18 _id isn’t legal because of the underscore, class isn’t legal because it’s a keyword, and two attributes can’t have the same value.

01:27 Using this variable as the list of attributes would fail for several different reasons.

01:39 My attempt at creating a Passenger class causes a ValueError. It’s kind of interesting that it found the keyword as the first problem rather than the underscore. It’s a bit revealing about the underlying implementation.

01:50 Anyway, to get around this problem, you can use the rename argument to the factory, setting it to True.

02:05 This time the Passenger class was created. Let’s create a passenger …

02:17 and now I’ll look at them. Notice that the three fields that had problem names have special names in this tuple, using underscore position. Since you can’t create fields with leading underscores, these are guaranteed not to clash with other fields.

02:33 This isn’t the prettiest solution, as you’re losing the naming feature and essentially back to a hack on positional access, but it does mean your data processing code doesn’t have to change.

02:44 You can stick with the column names of your file or database. In addition to renaming fields, you can also provide a default value for them. You do this with the defaults argument to the factory.

03:09 Here I’ve specified two defaults, the values "Junior" and "Python". The defaults lists start from the right-hand side of the arguments.

03:18 This named tuple has three fields. Giving it two defaults means the first argument is required, and the second and third have default values. This feels a bit messy to me.

03:29 I’m not sure that if I hadn’t read the instructions that I’d expect this behavior, but it is similar to how you specify default values to the arguments of a function. For example, in a function with three arguments, you can’t only give the second one a default.

03:44 Let me create a instance of a developer, and since I only specified the name, the "Junior" and "Python" defaults get applied. As the name implies, they’re only defaults, and since they get applied in order, when I give two arguments to the constructor,

04:06 only the .level attribute uses the default value. In the previous lesson, when I showed you the type of the class returned from the named tuple factory, you saw that the module name was __main__.

04:19 That’s two underscores, main, and then two underscores. Some Python tools can get particular about the module name. Pickling, for example, cares about it. If you need to work in this space, the named tuple factory gives you a way to override the module name for the resulting class. Before I show you how to do this, just a quick side note: .__module__ on a class tells you what module the class is found in. Let me show you an example.

04:50 I’m choosing OrderedDict because it’s a class from a module … and there’s the .__module__ attribute. The named tuple factory allows you to set this value to whatever you like.

05:04 You do this with the module argument to the factory.

05:14 I’ve set this one to "custom", and when I look at the class, it shows as being part of the custom module. Just like the OrderedDict example, I can see this in the .__module__ attribute.

05:33 Named tuple classes also come with utility methods and attributes. I’ll show you these in the next lesson.

Become a Member to join the conversation.