python 判断一个类或实例是否继承自某类

def ischildof(obj, cls):

    try:

        for i in obj.__bases__:

            if i is cls or isinstance(i, cls):

                return True

        for i in obj.__bases__:

            if ischildof(i, cls):

                return True

    except AttributeError:

        return ischildof(obj.__class__, cls)

    return False



isSubClassOf = ischildof

简单测试可行

你可能感兴趣的:(python)