Created
January 14, 2020 19:32
-
-
Save RyanRLong/4bae7f767c8fab71901bf319d73de48b to your computer and use it in GitHub Desktop.
Treat a python dictionary as an object
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 DictObject(dict): | |
| def __init__(self, dict_data): | |
| super().__init__(dict_data) | |
| def __dir__(self): | |
| return super().__dir__() + [str(k) for k in self.keys()] | |
| def __repr__(self): | |
| return f"<DictObject:}>" | |
| def __getattr__(self, item): | |
| return self[item] | |
| @property | |
| def computer_property(self): | |
| return "I am a computed property" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment