Reply – Re: Comparing classes or construction object
Your Name
Subject
Message
or Cancel
In Reply To
Re: Comparing classes or construction object
— by William William
Hi ~

I defined two functions. I hope that they would be helpful. They works well in most cases.

def is_instance_of(i, c):
    # i: Thing, c:Class
    if isinstance(c, And):
        return all(is_instance_of(i, cc) for cc in c.Classes)
    elif isinstance(c, Or):
        return any(is_instance_of(i, cc) for cc in c.Classes)
    else:
        return c in i.is_instance_of or any(is_a(y, c) for y in i.INDIRECT_is_instance_of if hasattr(y, 'is_a'))

def is_a(x, c):
    # x,c: Class
    if isinstance(c, And):
        return all(is_a(i, cc) for cc in c.Classes)
    elif isinstance(c, Or):
        return any(is_a(i, cc) for cc in c.Classes)
    else:
        return c in x.is_a or any(is_a(y, c) for y in x.is_a if hasattr(y, 'is_a'))