Skip to content

Instantly share code, notes, and snippets.

@kirsle
Created July 18, 2014 21:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirsle/28a0c1e5833f41586c78 to your computer and use it in GitHub Desktop.
Save kirsle/28a0c1e5833f41586c78 to your computer and use it in GitHub Desktop.
AttrDict - A dict for Python that behaves like JavaScript.
#!/usr/bin/env python
"""A dict-like object that supports JS object syntax.
AttrDict is a dict that can be called using either square brackets, like a
normal dict, or by using attributes--just like in JavaScript.
Accessing the value for an undefined key will return None (Python's equivalent
to undefined).
Just a toy. Don't use in production. ;)
--Kirsle"""
class AttrDict(object):
def __init__(self, **kwargs):
self.__dict__.update(dict(**kwargs))
def __getattr__(self, name):
return self.get(name)
def __getitem__(self, name):
return self.get(name)
def get(self, name, default=None):
return self.__dict__.get(name, default)
def __setattr__(self, name, value):
self.__dict__[name] = value
def __setitem__(self, name, value):
self.__dict__[name] = value
def __delattr__(self, name):
del self.__dict__[name]
def __delitem__(self, name):
del self.__dict__[name]
def __str__(self):
return str(dict(self.__dict__))
def __repr__(self):
return "AttrDict({})".format(repr(self.__dict__))
test = AttrDict(hello="world")
print test.hello
test.goodbye = "mars"
print test.goodbye
print test["hello"]
print test
print repr(test)
del test["hello"]
del test.goodbye
test["abc"] = "123"
print test.get("abc")
print test.get("badkey", "default")
test.hello = AttrDict()
test.hello.world = AttrDict()
test.hello.world.key = "value"
print test.hello.world.key
print test["hello"].world["key"]
print test["hello"]["world"].key
print repr(test)
# Bad keys return None
print test.badkey
print test["badkey"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment