I'm using a Javascript-style dictionary as discussed here. Implementation:
class DotDict(dict):
def __getattr__(self, attr):
return self.get(attr, None)
__setattr__= dict.__setitem__
__delattr__= dict.__delitem__
I've used this structure for some time with no issues, but recently needed a dictionary with a hyphenated key, like this:
foo = DotDict()
foo.a = 'a' #Business as usual
foo.a-b = 'ab' #Broken
Assigning to foo.a-b results in:
SyntaxError: can't assign to operator
This breaks because the '-' is seen as a minus operation and not as part of the key name. Is there another way to create a dictionary with dot-style member access?