Created
August 31, 2018 13:45
-
-
Save theelous3/b193c61af55ef87b4baf0b1039bfb62c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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