def rgetattr(obj, attr, *args):
    """ A recirsive implementation of getattr. Accepts dotted attributes attr1.attr2.attr3...

    :param obj:
    :param attr:
    :return:
    """
    if "." not in attr:
        if args:
            return getattr(obj, attr, args[0])
        else:
            return getattr(obj, attr)
    else:
        split_attrs = attr.split('.')

        if args:
            return rgetattr(getattr(obj, split_attrs[0]), '.'.join(split_attrs[1:]), args[0])
        else:
            return rgetattr(getattr(obj, split_attrs[0]), '.'.join(split_attrs[1:]))