Created
January 22, 2013 21:59
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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