Skip to content

Instantly share code, notes, and snippets.

@SavvyGuard
Created September 11, 2013 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SavvyGuard/6529230 to your computer and use it in GitHub Desktop.
Save SavvyGuard/6529230 to your computer and use it in GitHub Desktop.
Python dict as a class
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