1

I have a number of classes that I am passing around my code as objects prior to instantiating them. I'm trying to test the type of the class in a similar way to testing the type of an object instance to workout which one to use. I should mention these classes can be subclasses of each other.

Unfortunately, using isinstance(MyClass, MyClass) in this manner on the class itself returns False. According to the docs class objects are of type type.

So my question is - Is it possible for me to workout the class type in the same way you can with instance types? How would I go about this?

donopj2
  • 3,933
  • 5
  • 23
  • 30
  • 3
    Why do you want to do this? Use [duck typing](http://www.voidspace.org.uk/python/articles/duck_typing.shtml). –  Jun 14 '13 at 09:49

1 Answers1

4

What exactly are you trying to do?

If you want to know whether class A is a subclass of a class B you could just use issubclass:

issubclass(A, B)

For your particular case, you could just do Myclass == MyClass or even MyClass is MyClass.

In general, deep introspection is not such a good idea, and is usually left for generic frameworks like Django.

George Karpenkov
  • 2,094
  • 1
  • 16
  • 36