Created
November 13, 2015 19:02
-
-
Save elsonidoq/1a917eddd63b1608f5b1 to your computer and use it in GitHub Desktop.
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
class mydefaultdict(dict): | |
def __init__(self, default, **kwargs): | |
super(mydefaultdict, self).__init__(**kwargs) | |
self.default = default | |
def __getitem__(self, item): | |
try: | |
return super(mydefaultdict, self).__getitem__(item) | |
except KeyError: | |
val = self.default(item) | |
self[item] = val | |
return val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/msemelman/7fd64ec2e3eb3781df69