Skip to content

Instantly share code, notes, and snippets.



Last active Aug 29, 2015
What would you like to do?
Python Nested Dictionary
class Nil(object):
def __getitem__(self, key):
return Nil()
def __nonzero__(self):
return False
class NestedDict(dict):
def __init__(self, *args, **kwargs):
self.update(*args, **kwargs)
def __missing__(self, key):
return Nil()
def __setitem__(self, key, val):
if isinstance(val, dict): val = NestedDict(val)
dict.__setitem__(self, key, val)
def update(self, *args, **kwargs):
for k, v in dict(*args, **kwargs).iteritems():
self[k] = v
x = NestedDict({ '1': { '2': { '3': {'4': { '5': 'eran' }}}} })
if x['1']['2']['impossible!']:
print 'Its impossible we print here!'
print 'all good'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.