Last active
December 3, 2018 11:50
-
-
Save sparkydogX/9a5cf522ea672b760350de89c3896aa9 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 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