class Struct(dict):
"""
Object-Dict
>>> o = Struct({'a':1})
>>> o.a
>>> 1
>>> o.b
>>> None
"""
def __init__(self, *e, **f):
if e:
self.update(e[0])
if f:
self.update(f)
def __getattr__(self, name):
# Pickle is trying to get state from your object, and dict doesn't implement it.
# Your __getattr__ is being called with "__getstate__" to find that magic method,
# and returning None instead of raising AttributeError as it should.
if name.startswith('__'):
raise AttributeError
return self.get(name)
def __setattr__(self, name, val):
self[name] = val
def __delattr__(self, name):
self.pop(name, None)
def __hash__(self):
return id(self)
def copy(self):
return Struct(dict.copy(self))
a_dict=dict(a=2, b=3)
a_dict=Struct(a_dict)
b_dict=Struct(a=2, b=3)
print(a_dict.a) # 2
print(b_dict.a) # 2