Skip to content

Instantly share code, notes, and snippets.

@imcomking
Created September 19, 2018 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imcomking/d6c3ab1f8dbceee8a5e5b03b5958ce44 to your computer and use it in GitHub Desktop.
Save imcomking/d6c3ab1f8dbceee8a5e5b03b5958ce44 to your computer and use it in GitHub Desktop.
python dictionary dot access class
class Map(dict):
def __init__(self, *args, **kwargs):
super(Map, self).__init__(*args, **kwargs)
for arg in args:
if isinstance(arg, dict):
for k, v in arg.items():
self[k] = v
if kwargs:
for k, v in kwargs.items():
self[k] = v
def __getattr__(self, attr):
return self.get(attr)
def __setattr__(self, key, value):
self.__setitem__(key, value)
def __setitem__(self, key, value):
super(Map, self).__setitem__(key, value)
self.__dict__.update({key: value})
def __delattr__(self, item):
self.__delitem__(item)
def __delitem__(self, key):
super(Map, self).__delitem__(key)
del self.__dict__[key]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment