Skip to content

Instantly share code, notes, and snippets.

@elsonidoq
Created November 13, 2015 19:02
Show Gist options
  • Save elsonidoq/1a917eddd63b1608f5b1 to your computer and use it in GitHub Desktop.
Save elsonidoq/1a917eddd63b1608f5b1 to your computer and use it in GitHub Desktop.
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
@msemelman
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment