Created
November 30, 2011 16:26
-
-
Save jbochi/1409684 to your computer and use it in GitHub Desktop.
Cool dict
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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