Skip to content

Instantly share code, notes, and snippets.

@sakurai-youhei
Created October 21, 2021 00:07
Show Gist options
  • Save sakurai-youhei/e86a62ad4cc141e18b6568f0a5cbd018 to your computer and use it in GitHub Desktop.
Save sakurai-youhei/e86a62ad4cc141e18b6568f0a5cbd018 to your computer and use it in GitHub Desktop.
Nested arbitrary dict in Python
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