Skip to content

Instantly share code, notes, and snippets.

@danielhfrank
Created January 22, 2013 21:59
Show Gist options
  • Save danielhfrank/4598900 to your computer and use it in GitHub Desktop.
Save danielhfrank/4598900 to your computer and use it in GitHub Desktop.
Extend `collections.defaultdict` to actually be useful and call its `default_factory` with the missing key as an argument. Essentially just a concise way of memoizing the `default_factory` function.
from collections import defaultdict
class ArgDefaultDict(defaultdict):
def __missing__(self, key):
self[key] = self.default_factory(key)
return self[key]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment