Created
October 21, 2021 00:07
-
-
Save sakurai-youhei/e86a62ad4cc141e18b6568f0a5cbd018 to your computer and use it in GitHub Desktop.
Nested arbitrary dict in Python
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
ndict = type("ndict", (dict, ), {"__getitem__": lambda *args: dict.setdefault(*args, ndict())}) | |
intro = ndict() | |
intro['here']['is']['my name'] = 'Youhei Sakurai' | |
intro['here']['is']['my favorite'] = 'Python' | |
print(intro) # Outputs: {'here': {'is': {'my name': 'Youhei Sakurai', 'my favorite': 'Python'}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment