-
Notifications
You must be signed in to change notification settings - Fork 194
Closed
Labels
Description
https://hackernoon.com/python-stories-september-2018-d34e526edde2
collections.defaultdict allows you to create a dictionary that returns the default value if the requested key is missing (instead of raising KeyError). To create a defaultdict you should provide not a default value but a factory of such values.
That allows you to create a dictionary that virtually contains infinite levels of nested dicts, allowing you to do something like d[a][b][c]...[z].
>>> def infinite_dict():
... return defaultdict(infinite_dict)
...
>>> d = infinite_dict()
>>> d[1][2][3][4] = 10
>>> dict(d[1][2][3][5])
{}
Such behavior is called “autovivification”, the term came from the Perl language.
Having the transport return one of these type of dictionaries would likely solve a lot of the cli bugs we get when data from the api doesn't exist like we would expect.