Providing Optional Factory Arguments
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.
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
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.
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.
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.
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: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.
Let me create a instance of a developer, and since I only specified the name, the
"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,
.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
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.
Become a Member to join the conversation.