Created
April 30, 2015 17:18
-
-
Save dirn/4c60ff53b2eb83bb0dc6 to your computer and use it in GitHub Desktop.
This file contains 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 AliasedDict(dict): | |
def __init__(self, mapping): | |
self.mapping = mapping | |
def __getitem__(self, key): | |
if key in self.mapping: | |
key = self.mapping[key] | |
return super().__getitem__(key) | |
def __setitem__(self, key, value): | |
if key in self.mapping: | |
key = self.mapping[key] | |
return super().__setitem__(key, value) | |
if __name__ == '__main__': | |
d = AliasedDict({'a': 'b', 'c': 'd'}) | |
d['a'] = 1 | |
d['c'] = 2 | |
print(d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment