namedtuple

The namedtuple collection makes it convenient to store data in an expressive way. You can think of them as a hybrid of tuples and dictionaries, as they get the small memory footprint and immutability from the former and keyword access from the latter. They are very effective if you work with multiple data points of the same structure that have no bound logic, for example, users or goods in the eShop. Before we actually create an instance, we'll have to specify the properties of our future record (immutability, remember?). In the following code snippet, we create a data structure for users that contains the name, surname, and age properties. Once a named tuple is defined, there is no way to change it:

from collections import namedtuple
user = namedtuple('User', 'name, surname, age')

Now, we can add the instances, which are based on the previously defined tuples:

peter = user(name='Peter', surname='Pan', age=13) 
wendy = user(name='Wendy', surname='Darling', age=13)

One of the reasons namedtuple is efficient is that its argument structure is stored once, rather than for every instance. Once we have instances, their properties can be retrieved by using an index, slicing, or the name of the property:

>>> peter[0]
Peter

>>> peter[:2]
('Peter', 'Pan')

>>> wendy.surname
'Darling'

If for any reason, we need to retrieve a property by its name as a string value or test whether such an argument exists, we can use Python's built-in getattr and hasattr functions:

>>> hasattr(wendy, 'age')
True

>>> getattr(wendy, 'age')
13

A namedtuple collection is a class that inherits from a tuple; the resulting instances of the tuple are 100% normal class instances. You can see the code under the hood by passing a verbose=True argument. With that being said, Python 3.7 introduces data classes, which are pure Python classes that can solve a somewhat similar set of problems. They are a little more descriptive and flexible, although they take more memory than the earlier data classes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset