python - 如何在 Python 中比较对象的类型?

基本上我想这样做:

obj = 'str'
type ( obj ) == string

我试过了:

type ( obj ) == type ( string )

它没有用。

另外,其他类型呢?例如,我无法复制 NoneType

最佳答案

isinstance()

在您的情况下, isinstance("this is a string", str) 将返回 True

您可能还想阅读以下内容:http://www.canonical.org/~kragen/isinstance/

https://stackoverflow.com/questions/707674/

相关文章:

Linux - 替换文件名中的空格

linux - 如何获取 Docker 中依赖的子镜像列表?

linux - 如何从文本文件中删除换行符?

python - Python中的字符串如何格式化 boolean 值?

python - 如何使用 Python 发送电子邮件?

linux - 如何计算每个目录中的文件数?

android - Ubuntu - 错误 : Failed to create the SD ca

python - 导入语句python3的变化

python - 在 python 中创建线程

linux - 为什么在 Bash 中应该避免使用 eval,我应该使用什么来代替?