namedtuple
When writing scripts of any kind you will often come across data collections, or groupings that could really do with being stored in some sort of structure. Now, you can always go away and make a class, with a constructor and properties and so on and so forth… but this is scripting and that might feel a bit too much like hard work. Instead I ask if you have heard of namedtuple().
namedtuple() is a factory that can be imported from the collections package in the Python standard library. It is available in IronPython, Python 2.7, an all the way up to 3.10 and beyond. It creates simple named tuple classes for you to use! But… what are they? Well, they’re just tuples you can use. Except, these are “named”, by which I mean they have a type name and property names. For example, see below.
CPoint
typing.NamedTuple
If you’re working with modern Python then you already know about the wonder that is type hinting, and I can see some of you thinking “but how do you do typing??” and “dataclasses let me do type hinting though…” and as of Python 3.4 there is a nice way to do this.
Downsides of namedtuples
The attrs package has a nice section about named tuples and their weaknesses, which you should read here, and I summarise below with some of my own observations.
- named tuples are subclasses of tuples and so can be indexed and iterated over (this is very unusual behaviour for a class)
- You can unpack them by accident
- All built-in tuple methods will be available on your instance regardless
- You can’t navigate to the class definition easily in IDEs
- It is tricky to add documentation to your named tuple classes
“If you want a tuple with names, by all means: go for a namedtuple.”And the truth is that it is rare for there to be features in any language, which should never be used (except for GOTO obviously). Named tuples are a tool for scripting and should be treated as such. When scripting they can be really powerful.
An Example
Recently I needed to parse some mystery data, and the format was as follows.numpy to get a big 500 x 500 array. But what about the first 6 lines? That seems like it might be useful for interpreting the data. We need to create some variables, or a class, to store it. Additionally, this is data from one file, but in actuality I have 25. How do we keep them all together? We can use namedtuples!
Below I have composed a short script that parses all 25 files and stores their data in instances of a namedtuple class called ElevationFile. The only packages I use that are not part of the Python Standard Library are numpy and matplotlib, which are freely available and easy to install.
It’s elevation data! This data was taken from DEFRA UK data OGL v3.0. This is freely-available UK elevation data and if you google “Plover Hill” in Yorkshire and look at the elevation you’ll see something a lot like this. Processing csv data like this is a common occurrence for engineers and data scientists and using named tuples can really help you keep your code concise and readable. It won’t solve all your problems but it’s a tool for the ring and worth knowing about.