def call_on_if_def ( obj , attr_name , callable , default , * args , * * kwargs ) : try : attr = getattr ( obj , attr_name ) except AttributeError : return default else : return callable ( attr , * args , * * kwargs )
if attribute is calable python
Calls the provided callable on the provided attribute of obj if it is defined .
def call_on_if_def(obj, attr_name, callable, default, *args, **kwargs):
    """Calls the provided callable on the provided attribute of ``obj`` if it is defined.

    If not, returns default.
    """
    try:
        attr = getattr(obj, attr_name)
    except AttributeError:
        return default
    else:
        return callable(attr, *args, **kwargs)