Writing dictionary-related type hints
When we look at sets and lists, we generally expect each item within a list
(or a set
) to be the same type. When we look at object-oriented class designs, in Chapter 7, Basics of Classes and Objects, we'll see how a common superclass can be the common type for a closely related family of object types. While it's possible to have heterogeneous types in a list or set collection, it often becomes quite complex to process.
Dictionaries are used in a number of different ways.
- Homogeneous types for values: This is common for dictionaries based on
collections.Counter
orcollections.defaultdict
. The input from acsv.DictReader
will also be homogeneous, since every value from a CSV file is a string. - Heterogeneous types for values: This is common for dictionaries used to represent complex objects that have been serialized into JSON notation. It's also common for internal data structures created as part of more...