Writing Cleaner Code
00:11 There are several situations where you might choose to use a named tuple to make your code more readable. You’ve already seen how using a dot notation name can be better than a tuple index. If your function returns multiple values, returning a named tuple may make the result easier to understand.
Consider a variable that contains information about a line being drawn to your screen. This is sometimes called a pen. My vanilla tuple here contains a width, a style description, and a Boolean to indicate whether or not the bevel behavior is on. Now, pretend you’re further down in your source code, far away from the definition of
pen and the associated comment. You see the following.
One of Python’s built-in functions is
divmod(). My example here divides
4 and returns a tuple containing the quotient and the remainder. Any time you use
divmod(), you have to remember what is meant by each part of the tuple.
By returning a named tuple, my custom version of
divmod() is much clearer. The parts of the tuple are named, and so you only have to remember what a quotient is rather than guess at its purpose. This example is a bit contrived in order to keep the function short, but hopefully you get the idea.
Not only does it shorten the function signature, but within the function, it keeps the related pieces of data within the same object. The code here is a little unrealistic, as it’s all in one place so I could show you the idea. If you were actually using this, the capital-U
User class and the function would be in the same module, whereas the programmer calling the
create_user() function would likely be importing the
User class and the function into another file.
04:28 Before showing you some code, consider the following CSV file. The first line is a header with the names of the fields, and then there are three lines of data. Each line has a name, title, and email address. Let’s go play with this CSV in the REPL.
The Python standard library has a handy little module that helps you interact with CSV files. They went out on a limb and named it
csv. Yeah, I know. Crazy, huh? You can never trust those standard-library writers.
Seeing as I haven’t started iterating on the reader yet, the return value will be the first thing in the iterable. Recall the first row in the file was the header line containing the field names. As the named tuple factory needs a set of names for the fields, this is all rather convenient. By sending the first row of the file to the named tuple factory, I’m creating the
Employee named tuple class with three attributes:
rename argument for the named tuple factory.
06:38 Because I’ve set this value, if the CSV file happened to have a field name that wasn’t a legal tuple name, this code would still work. Okay. With the employee class created, I can now start processing the rest of the CSV file.
Wouldn’t it be much better to have a readable employee than having to remember that index
1 is their title? In the next lesson, I’ll walk you through how named tuples compare to other data structures in Python.
Become a Member to join the conversation.