from functools import reduce


def recursive_getattr(obj, attr, *args, delimiter="__"):
    """Recursive getattr on a object, based on the attr name, delimited with the delimiter."""

    def _getattr(obj, attr):
        rg = getattr(obj, attr, *args)

        # Usefull for getting attribute like `datetime__date` => will result in datetime.date()
        if callable(rg):
            return rg()

        return rg

    return reduce(_getattr, [obj] + attr.split(delimiter))
