Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Choosing a Record Type

Data Records, or plain data objects, are used to group related fields together in a single place. Python provides a number of ways of doing this grouping, including using the dict type, object oriented mechanisms such as classes and data classes, and the struct library’s Struct object for binary records.

Here are resources and additional documentation about records, dataclasses, and structs:

00:00 In the previous lesson, I showed you how to use data classes, the NamedTuple object, and structs in order to build yourself some records. In this lesson, I’m going to talk about how to choose between these different record types and point you at some documentation.

00:16 You can consider choosing the right record type as a stepwise process in how complex something is. If you need a record that is immutable and has a small number of fields, then a tuple is a good choice.

00:29 This is particularly common for numeric groupings, like a coordinate. So if I’m doing 2D or 3D graphics and I’m trying to do a point of (x, y) or (x, y, z), a tuple is a natural way of doing this.

00:43 As you increase the number of fields inside of this tuple, you should switch to a named tuple for clarity. Another reason to switch to named tuples is if you want to enforce the field names and make sure they’re there, rather than just being a positional information.

00:57 If your record needs to be mutable but is still fairly small and not used an awful lot in your code, then instead of a tuple you’ll want to go with a dict.

01:06 The dict is a very powerful and flexible object and because it’s used so much inside of Python, it has a very performant implementation.

01:15 It also has the added advantage of converting very easily to JSON, so if you’re doing any kind of web programming where you’re having to send information between Python and the browser—JSON’s the most common way of doing that—dictionaries make this easier.

01:29 As your record becomes more complicated or your needs for enforcement about fields become more complicated, then you’ll want to upgrade from a dict to either a class or a dataclass.

01:41 If the purpose of the class is just the data storage, then the dataclass is probably your best choice. If you’re going to start including an awful lot of methods anyways, then that’s where you’d move from the dataclass to a class. And finally, if your records are for binary data, then the struct library’s Struct object is the way to go.

02:01 If you want more information on data classes, the Python documentation is always a good place to start, or you can take a look at this article.

02:11 Here’s the link for the NamedTuple object and for the struct library.

02:18 That’s everything I’m going to cover for record data structures. In the next lesson, I’ll cover sets and multisets.

Become a Member to join the conversation.