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?

1
  • 3
    Why do you want to do this? Use duck typing. Commented Jun 14, 2013 at 9:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.