I was wondering how to check whether a variable is a class (not an instance!) or not.
I've tried to use the function
isinstance(object, class_or_type_or_tuple) to do this, but I don't know what type would a class will have.
For example, in the following code
class Foo: pass isinstance(Foo, **???**) # i want to make this return True.
I tried to substitute "
class" with ???, but I realized that
class is a keyword in python.
Even better: use the
>>> import inspect >>> class X(object): ... pass ... >>> inspect.isclass(X) True >>> x = X() >>> isinstance(x, X) True >>> y = 25 >>> isinstance(y, X) False
The inspect.isclass is probably the best solution, and it's really easy to see how it's actually implemented
def isclass(object): """Return true if the object is a class. Class objects provide these attributes: __doc__ documentation string __module__ name of module in which this class was defined""" return isinstance(object, (type, types.ClassType))