Created
April 26, 2018 23:58
-
-
Save AlexanderCollins/83bf3b2cd7620bb5d2df16e9a9fbb5a5 to your computer and use it in GitHub Desktop.
Python Dictionary, Instance Representation
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
# 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice stuff! Thanks for sharing it!