def _getattr(obj, attr, default=None):
try: left, right = attr.split('.', 1)
except: return getattr(obj, attr, default)
return _getattr(getattr(obj, left), right, default)
def _setattr(obj, attr, val):
try: left, right = attr.split('.', 1)
except: return setattr(obj, attr, val)
return _setattr(getattr(obj, left), right, val)
def _hasattr(obj, attr):
try: left, right = attr.split('.', 1)
except: return hasattr(obj, attr)
return _hasattr(getattr(obj, left), right)
class C(object): pass
a = C()
a.b = C()
a.b.c = 4
assert _getattr(a, 'b.c') == 4
_setattr(a, 'b.c', 2)
assert a.b.c == 2
assert _hasattr(a, 'b.c')
try: left, right = attr.split('.', 1)
except: return getattr(obj, attr, default)
return _getattr(getattr(obj, left), right, default)
def _setattr(obj, attr, val):
try: left, right = attr.split('.', 1)
except: return setattr(obj, attr, val)
return _setattr(getattr(obj, left), right, val)
def _hasattr(obj, attr):
try: left, right = attr.split('.', 1)
except: return hasattr(obj, attr)
return _hasattr(getattr(obj, left), right)
class C(object): pass
a = C()
a.b = C()
a.b.c = 4
assert _getattr(a, 'b.c') == 4
_setattr(a, 'b.c', 2)
assert a.b.c == 2
assert _hasattr(a, 'b.c')