Object-Oriented Design Frequently Asked Questions


Python

How does one check whether an object c is of "type" C ?

  1. If the object c is a literal and the type C a builtin type (such as an int) which cannot be instantiated, then the test

        c == int(c)
    can for example be used (as in the Roman Numeral example).

  2. If the object c is however an instance of a class, and we want to check if c is an instance of class C, we can use the test

       isinstance(c, C)
    This will evaluate to True (1) if c is an instance of class C or any of C's sub-classes.