How does python determine whether a specified class exists?

class A():
    pass

if xxxx("A"):
    print("A exists")
else:
    print("A not exists")
Mar.28,2021

you can use the method of comparing hacky:

def is_class_exists(class_name):

    if not class_name.isidentifier():
        return False

    try:
        return isinstance(eval(class_name), type)
    except NameError:
        return False
Menu