Comparing Data Structures
00:00 In the previous lesson, I showed you some common uses of named tuples and how they can make your code easier to read. This lesson contains a big showdown: named tuples against all comers, why you might choose it in comparison to other data structures.
00:17 Python comes with many data structures out of the box. So how do you know which to choose? Some things to consider are the principle of least surprise. This means use what other programmers would expect you to use.
00:47 Nobody expects it, and this isn’t the Happy Birthday kind of surprise, more the murdering clown in the closet kind of surprise. So in short, do what’s expected. The entire previous lesson was on readability, so I’ve probably already convinced you that that’s important. Tuples, named or otherwise, are immutable.
01:32 These two pieces of code accomplish similar things. In both cases, I get objects with named fields. So what are some of the differences that would affect your choice? Well, dictionaries are mutable, whereas named tuples are not.
01:47 Depending on what you want to do with your data, you may want either situation. Generally speaking, if you’re not going to change your objects, using an immutable class is better because if you accidentally try to change it, it will throw an exception. Instead, if you used a mutable object even though you weren’t planning on changing it, if you accidentally did, that might go undetected for a long time.
02:10 Dictionaries don’t support dot notation. I’ve never understood why. There are even classes out there allow this behavior, but they don’t. Named tuples require you to import a module, whereas dictionaries are built in. And finally, named tuples require less memory than an equivalent dictionary. For small objects, a named tuple can be less than half the size of a corresponding dict.
Like named tuples, data classes use dot notation for attribute access. But unlike named tuples, the fields are not iterable. You could implement your own version of
.__iter__() to make it so, but that requires extra work.
03:20 Data classes generally are pretty close in size to dictionaries and so are bigger than a named tuple. And performance-wise, the cost of creation of these two kinds of objects are about equivalent.
Instead of using a decorator like you do with a data class, you inherit from
NamedTuple, but like the data class, you specify the attributes to be found in the class. Once this is done, you can use this class as a named tuple. There’s our trusty
04:32 The final showdown is named tuples against the original tuple. Family squabbles are always the worst. Named tuples are tuples. You, of course, get the dot notation and the exact same memory footprint.
The performance characteristics are different, though. Creating a regular old tuple is about three times faster than the named variety. So if you’re doing a lot of data processing, you might want to stick with regular tuples. If you need to, you can always convert them to the named variety using the
._make() class method at the point in your code where performance isn’t as pressing.
Become a Member to join the conversation.