Additional Tuple Techniques
Here, we defined a collection of pairs. Then, we ranked the two tuples, assigning the
sequence of Rank_Data objects to the rank_x variable. We then ranked this collection
of Rank_Data objects, creating a second rank value and assigning the result to the
rank_xy variable.
The resulting sequence can be used to a slightly modified rank_corr() function
to compute the rank correlations of any of the available values in the rank_seq
attribute of the Rank_Data objects. We'll leave this modification as an exercise
for the readers.
Summary
In this chapter, we looked at different ways to use namedtuple objects to implement
more complex data structures. The essential features of a namedtuple are a good fit
with functional design. They can be created with a creation function and accessed by
position as well as name.
We looked at how to use immutable namedtuples instead of stateful object
definitions. The core technique was to wrap an object in an immutable tuple to
provide additional attribute values.
We also looked at ways to handle multiple data types in Python. For most arithmetic
operations, Python's internal method dispatch locates proper implementations. To
work with collections, however, we might want to handle iterators and sequences
slightly differently.
In the next two chapters, we'll look at the itertools module. This library module
provides a number of functions that help us work with iterators in sophisticated
ways. Many of these tools are examples of higher-order functions. They can help
make a functional design stay succinct and expressive.