Python :NamedTuple
Let’s discover namedtuple and its functions.
We’ll cover the following
- Overview of namedtuple
- Creating namedtuple
- How namedtuple helps in keeping track of item’s index?
Overview of namedtuple
Let’s discuss the namedtuple
class from the collection module which we can use to replace Python’s tuple. Of course, the namedtuple
is not a drop-in replacement as we will see soon. We have seen some programmers use it like a struct. If we haven’t used a language with a struct in it, then that needs a little explanation. A struct is basically a complex data type that groups a list of variables under one name.
Creating namedtuple
Let’s look at an example of how to create a namedtuple
so we can see how they work:
Here we import namedtuple
from the collections
module. Then we called namedtuple
, which will return a new subclass of a tuple but with named fields. So basically we just created a new tuple class. We will note that we have a strange string as our second argument. This is a space-delimited list of properties that we want to create.
Now that we have our shiny new class, let’s create an instance of it! As we can see above, we do that as our very next step when we create the auto_parts
object. Now we can access the various items in our auto_parts
using dot notation because they are now properties of our Parts
class.
How namedtuple
helps in keeping track of item’s index?
One of the benefits of using a namedtuple
over a regular tuple is that we no longer have to keep track of each item’s index because now each item is named and accessed via a class property. Here’s the difference in code:
In the code above, we create a regular tuple and access the cost of the vehicle engine by telling Python the appropriate index we want. Alternatively, we can also extract everything from the tuple using multiple assignment. Personally, we prefer the namedtuple
approach just because it fits the mind easier and we can use Python’s dir()
method to inspect the tuple and find out its properties. Give that a try and see what happens!
Here is a code to convert a Python dictionary into an object:
This is some weird code, so let’s take it one piece at a time. In the first line, we import namedtuple
as before. Next we create a Parts
dictionary. So far, so good. Now we’re ready for the weird part. Here we create our namedtuple
class and name it ‘Parts’. The second argument is a list of the keys from our dictionary. The last piece is this strange piece of code: (**Parts)
(line 5). The double asterisk means that we are calling our class using keyword arguments, which in this case is our dictionary. We could split this line into two parts to make it a little clearer:
So here we do the same thing as before, except that we create the class first, then we call the class with our dictionary to create an object. The only other piece we want to mention is that namedtuple
also accepts a verbose argument and a rename argument. The verbose argument is a flag that will print out class definition right before it’s built if we set it to True
. The rename
argument is useful if we’re creating our namedtuple
from a database or some other system that our program doesn’t control as it will automatically rename the properties for us.