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?
-
3Why do you want to do this? Use duck typing.user1907906– user19079062013年06月14日 09:49:09 +00:00Commented Jun 14, 2013 at 9:49
1 Answer 1
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.