Home  >  Article  >  Backend Development  >  Python isinstance判断对象类型

Python isinstance判断对象类型

WBOY
WBOYOriginal
2016-06-16 08:47:381574browse
复制代码 代码如下:

if (typeof(objA) == typeof(String))
{
//TODO
}

在Python中只需要使用内置的函数isinstance,使用起来非常简单,比如下面的例子:

复制代码 代码如下:

class objA:
pass

A = objA()
B = 'a','v'
C = 'a string'

print isinstance(A, objA)
print isinstance(B, tuple)
print isinstance(C, basestring)
输出结果:
True
True
True
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Python splitlines使用技巧Next article:Python字符转换