A nested dictionary which creates subnotes on the fly.
1 2 3 4 5 6 7 8 9
class auto_dict(dict): def __getitem__(self, key): return self.setdefault(key, self.__class__()) if __name__ == "__main__": d = auto_dict() d["foo"]["bar"]["baz"] = 42 print d
After reading an article about autovivication hashes in Ruby, I tried to find a simpler solution for Python.
I want a nested dictionary like objects, which can be indexed in multiple dimensions and which elements are automatically created, when accessed.
The implementation I used before is about 10 lines of code, this one is shorter and imho more elegant.
It used the setdefault() method of a dictionay to add another instance of self.__class__ as the default value for every key.
If there is no need for deeply nested dicts defaultdict to the rescue:
A small note of caution
The form using __missing__ only works with Python 2.5.x and higher!
In older Python versions it silently fails, throwing KeyError exceptions when the expected intermediate keys have not been created as expected.