Skip to content

Instantly share code, notes, and snippets.

@sparkydogX
Last active December 3, 2018 11:50
Show Gist options
  • Save sparkydogX/9a5cf522ea672b760350de89c3896aa9 to your computer and use it in GitHub Desktop.
Save sparkydogX/9a5cf522ea672b760350de89c3896aa9 to your computer and use it in GitHub Desktop.
自动实现字典嵌套
class Vividict(dict):
def __missing__(self, key):
value = self[key] = type(self)()
return value
def walk(self):
for key, value in self.items():
if isinstance(value, Vividict):
for tup in value.walk():
yield (key,) + tup
else:
yield key, value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment