Skip to content

Instantly share code, notes, and snippets.

@AlexanderCollins
Created April 26, 2018 23:58
Show Gist options
  • Save AlexanderCollins/83bf3b2cd7620bb5d2df16e9a9fbb5a5 to your computer and use it in GitHub Desktop.
Save AlexanderCollins/83bf3b2cd7620bb5d2df16e9a9fbb5a5 to your computer and use it in GitHub Desktop.
Python Dictionary, Instance Representation
# Represents a dictionary as an instance (note: cannot yet cast bact to dict type, simple access <InstanceDict>.d
class InstanceDict:
def __init__(self, d):
self.d = d
def __getattribute__(self, item):
return self.__getattr__(item)
def __getattr__(self, key):
val = self.d[key]
if isinstance(val, dict):
return InstanceDict(val)
return val
def __repr__(self):
return str(self.d)
def keys(self):
return self.d.keys()
@Munduruca
Copy link

Nice stuff! Thanks for sharing it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment