Skip to content

Instantly share code, notes, and snippets.

@jbochi
Created November 30, 2011 16:26
Show Gist options
  • Save jbochi/1409684 to your computer and use it in GitHub Desktop.
Save jbochi/1409684 to your computer and use it in GitHub Desktop.
Cool dict
>>> class CoolDict(dict):
... def __getattr__(self, attr):
... return self[attr]
...
>>> c = CoolDict()
>>> c['a']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'a'
>>> c['a'] = 3
>>> c['a']
3
>>> c.a
3
>>> CoolDict(a=3, b=3)
{'a': 3, 'b': 3}
>>> _.a
3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment