Skip to content

Instantly share code, notes, and snippets.

@theelous3
Created August 31, 2018 13:45
Show Gist options
  • Save theelous3/b193c61af55ef87b4baf0b1039bfb62c to your computer and use it in GitHub Desktop.
Save theelous3/b193c61af55ef87b4baf0b1039bfb62c to your computer and use it in GitHub Desktop.
from collections import OrderedDict
class OrderedDefaultDict(OrderedDict):
def __init__(self, default, *args, **kwargs):
# Your default must be callable.
self.default = default
super().__init__(*args, **kwargs)
def __missing__(self, key):
self[key] = value = self.default()
return value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment