Came up with this way, note: the class you are checking must be in globals though:
import inspect
def isinstance_string(variable, string):
cls = globals().get(string, None)
class Unused:
pass
cls = cls or Unused
if inspect.isclass(cls):
return isinstance(variable, cls)
return False
class A: pass
class B(A): pass
class C(B): pass
c_inst = C()
ok = isinstance_string(c_inst, 'A')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…