Skip to content

Instantly share code, notes, and snippets.

@Kwpolska
Last active December 30, 2015 07:09
Show Gist options
  • Save Kwpolska/7794400 to your computer and use it in GitHub Desktop.
Save Kwpolska/7794400 to your computer and use it in GitHub Desktop.
KeyClass — A class with an optional dict-like interface.
# 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