How to identify whether a variable is a class or an object
Question: I am working at a bit lower level writing a small framework for creating test
fixtures for my project in Python. In this I want to find out whether a
particular variable is an instance of a certain class or a class itself and if
it is a class, I want to know if it is a subclass of a certain class defined
by my framework. How do I do it?

    
    
    class MyBase(object):
        pass
    
    class A(MyBase):
        a1 = 'Val1'
        a2 = 'Val2'
    
    class B(MyBase):
        a1 = 'Val3'
        a2 = A
    

I want to find out if the properties a1 and a2 are instances of a class/type
(a1 is a string type in B) or a class object itself (i.e a2 is A in B). Can
you please help me how do I find this out?


Answer: Use the [inspect module](http://docs.python.org/library/inspect.html#module-
inspect).

> The inspect module provides several useful functions to help get information
> about live objects such as modules, classes, methods, functions, tracebacks,
> frame objects, and code objects. For example, it can help you examine the
> contents of a class, retrieve the source code of a method, extract and
> format the argument list for a function, or get all the information you need
> to display a detailed traceback.

For example, the
[`inspect.isclass()`](http://docs.python.org/library/inspect.html#inspect.isclass)
function returns true if the object is a class:

    
    
    >>> import inspect
    >>> inspect.isclass(inspect)
    False
    >>> inspect.isclass(inspect.ArgInfo)
    True
    >>>
    

