Skip to content

Instantly share code, notes, and snippets.

@dirn
Created April 30, 2015 17:18
Show Gist options
  • Save dirn/4c60ff53b2eb83bb0dc6 to your computer and use it in GitHub Desktop.
Save dirn/4c60ff53b2eb83bb0dc6 to your computer and use it in GitHub Desktop.
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