Skip to content

Instantly share code, notes, and snippets.

@nod
Created January 11, 2018 19:28
Show Gist options
  • Save nod/54cbcd53560190a7b52dd3148ceb4d43 to your computer and use it in GitHub Desktop.
Save nod/54cbcd53560190a7b52dd3148ceb4d43 to your computer and use it in GitHub Desktop.
>>> sample = {"a":2,"b":{"c":44}}
>>> sample.get("b",{}).get("c") # is gross
>>>
>>> class nestdict(dict):
... def __floordiv__(self, k):
... v = self.get(k)
... if isinstance(v, dict): return nestdict(v)
... return v
...
>>> z = nestdict(sample)
>>> z // "a"
2
>>> z // "b"
{'c': 44}
>>> z // "b" // "c"
44
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment