python - 如何检查变量是否是类?

我想知道如何检查变量是否是类(不是实例!)。

我尝试使用函数 isinstance(object, class_or_type_or_tuple) 来执行此操作,但我不知道类将具有什么类型。

例如在下面的代码中

class Foo: pass  
isinstance(Foo, **???**) # i want to make this return True.

我尝试用 ??? 替换“class”,但我意识到 class 是 python 中的关键字。

最佳答案

更好:使用 inspect.isclass功能。

>>> import inspect
>>> class X(object):
...     pass
... 
>>> inspect.isclass(X)
True

>>> x = X()
>>> isinstance(x, X)
True
>>> inspect.isclass(x)
False

https://stackoverflow.com/questions/395735/

相关文章:

python - 在 Python 中,如果我在 "with" block 内返回,文件还会关闭吗?

linux - 如何递归查找并列出具有子目录和时间的目录中的最新修改文件

python - 从 Numpy 数组 : How do I specify the index c

linux - 如何修复 'sudo: no tty present and no askpass

python - 为什么 Python 没有符号函数?

python - 如何逐行读取大型文本文件,而不将它们加载到内存中?

python - 异常后如何重试?

linux - 如何在linux命令行中替换多个文件中的字符串

linux - Bash 忽略特定命令的错误

linux - ./配置 :/bin/sh^M : bad interpreter