Here's the pattern I'm thinking of using:
class Dicty(dict):
def __init__(self):
self.__dict__ = self
d = Dicty()
d.foo = 'bar'
print d['foo']
>>> bar
d['foo'] = 'baz'
print d.foo
>>> 'baz'
Generally, I prefer the semantics of object attribute access over dict get/set access, but there are some circumstances where dict-like access is required (for example, d['foo-bar'] = 'baz') and I'd prefer not to have special getter setter methods for these cases, so thus, the dual behavior of dict & object at the same time with shared attributes.
Are there any gotchas with the above pattern?
isinstance(dict, object). You don't need to inherit fromobject. And the implementation is more than hacky. Also: possible duplicate of Accessing dict keys like an attribute in Python?