Determines whether the cls is the senior subclass of basecls for obj.

    The most senior subclass is the first class in the mro which is a subclass
    of testcls.

    Use for inheritance schemes where a method should only be called once by the
    most senior subclass.

    Don't use if it is optional to call the base method!

    In that case, the most reasonable way to do it is to ensure the method is
    idempotent and deal with the hopefully small inefficiency of calling it
    multiple times. Doing a hasattr check in these implementations should be
    relatively efficient.

    In particular, __init__ for mixins tends to act like this.

def is_senior_subclass(obj, cls, testcls):
    """Determines whether the cls is the senior subclass of basecls for obj.

    The most senior subclass is the first class in the mro which is a subclass
    of testcls.

    Use for inheritance schemes where a method should only be called once by the
    most senior subclass.

    Don't use if it is optional to call the base method!

    In that case, the most reasonable way to do it is to ensure the method is
    idempotent and deal with the hopefully small inefficiency of calling it
    multiple times. Doing a hasattr check in these implementations should be
    relatively efficient.

    In particular, __init__ for mixins tends to act like this.
    """
    for base in obj.__class__.mro():
        if base is cls:
            return True
        else:
            if issubclass(base, testcls):
                return False