[python] attribute fetch

def class_lookup(cls, name):
    v = cls.__dict__.get(name)
    if v is not None:
        return v, cls
    for i in cls.__bases__:
        v, c = class_lookup(i, name)
        if v is not None:
            return v, c
    return None


def object_getattr(obj, name):
    v, cls = class_lookup(obj.__class__, name)
    if (v is not None) and hasattr(v, '__get__') and hasattr(v, '__set__'):
        return v.__get__(obj, cls)
    w = obj.__dict__.get(name)
    if w is not None:
        return w
    if v is not None:
        if hasattr(v, '__get__'):
            return v.__get__(obj, cls)
        else:
            return v
    raise AttributeError(obj.__class__, name)

你可能感兴趣的:(python)