May 2018
Intermediate to advanced
380 pages
9h 37m
English
Another dictionary subclass, defaultdict calls a factory function to provide missing values; basically, it creates any items that you try to access, but only if they don't currently exist. This way, you don't get KeyError when trying to access a non-existent key.
All the standard dictionary methods are available, as well as the following:
__missing__(key): This method is used by the dict class __getitem__() method when the requested key is not found. Whatever key it returns (or an exception if no key is present) is passed to __getitem__(), which processes it accordingly.
Assuming the default_factory is not None, this method calls the factory to receive a default value for key, which is then placed in the dictionary ...