Skip to content

Instantly share code, notes, and snippets.

@668
Forked from deepgully/magicdict.py
Created August 14, 2014 20:22
Show Gist options
  • Save 668/a19e928017d3a02ad170 to your computer and use it in GitHub Desktop.
Save 668/a19e928017d3a02ad170 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
class MagicDict(dict):
def __init__(self, **kwargs):
dict.__init__(self, kwargs)
self.__dict__ = self
def __getattr__(self, name):
self[name] = MagicDict()
return self[name]
if __name__ == "__main__":
c = MagicDict()
c.people.name = 'roy'
c.test1.test2.test3 = "test3"
print(c)
print(c.people)
print(c.people.name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment