Created
September 11, 2013 20:19
-
-
Save SavvyGuard/6529230 to your computer and use it in GitHub Desktop.
Python dict as a class
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 ExDict(dict): | |
""" | |
Extended Dictionary | |
""" | |
def __init__(self,*arg,**kw): | |
super( ExDict , self).__init__(*arg, **kw) | |
def __setattr__(self, k, v): | |
if k in self.keys(): | |
self[k] = v | |
elif not hasattr(self, k): | |
self[k] = v | |
else: | |
raise AttributeError, "Cannot set '%s', cls attribute already exists" % ( k, ) | |
def __getattr__(self, k): | |
if k in self.keys(): | |
return self[k] | |
raise AttributeError, "Cannot fetch '%s', cls attribute does not exist" % ( k, ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment