How to know if a variable is either a class or a type in Python 3
07:29 14 May 2026

Sorry, I am not an expert in Python, but I am struggling with it.

I am just trying to determine if a variable is suitable for identifying a type or a class for using it safely at isinstance(my_object_instance, supposedly_the_expected_class_or_type).

This way:

$ python3
Python 3.12.9 (main, Feb 21 2025, 21:28:59) [GCC 12.4.0] on cygwin
>>> from typing import Sequence
>>> my_object_instance = (1, 2)
>>> supposedly_the_expected_class_or_type = Sequence
>>> print(isinstance(my_object_instance, supposedly_the_expected_class_or_type))
True

In order to avoid something like this:

>>> from typing import Sequence
>>> my_object_instance = (1, 2)
>>> supposedly_the_expected_class_or_type = "Sequence"
>>> print(isinstance(my_object_instance, supposedly_the_expected_class_or_type))
Traceback (most recent call last):
  File "", line 1, in 
TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union

As we can see that the direct check is not working:

>>> print(isinstance(Sequence, type))
False

Other attempts for checking the suitability:

>>> import typing
>>> print(type(Sequence))

>>> print(isinstance(Sequence, typing._SpecialGenericAlias))
True

But it does not seem to be very standard.

I am looking for a standard function like:

is_class_or_type(my_parameter) which returns True if that parameter represents a class or a type, and False otherwise

Does anyone know how to check that condition?

python