Last active
December 30, 2015 07:09
-
-
Save Kwpolska/7794400 to your computer and use it in GitHub Desktop.
KeyClass — A class with an optional dict-like interface.
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
# Copyright © 2013, Chris “Kwpolska” Warrick <[email protected]>. | |
# Licensed under the 3-clause BSD license, see: | |
# https://github.com/Kwpolska/python-project-template/blob/master/LICENSE | |
class KeyClass(object): | |
""" | |
A class with an optional dict-like interface. | |
spam = KeyClass() | |
spam.eggs == spam['eggs'] | |
spam.eggs = bacon == spam['eggs'] = bacon | |
""" | |
def __getitem__(self, item): | |
"""Get the attribute.""" | |
return getattr(self, item) | |
def __setitem__(self, item, value): | |
"""Set an attribute.""" | |
return setattr(self, item, value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment