Python天天美味(4) - isinstance判断对象类型

Python中判度对象类型方法非常简单,不需要像别的语言一样使用如下的判断方法:

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

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

class objA:
    
pass

= objA()
= 'a','v'
= 'a string'

print isinstance(A, objA)
print isinstance(B, tuple)
print isinstance(C, basestring)

输出结果:
True
True

True

Python 天天美味系列(总)

Python 天天美味(1) - 交换变量

Python 天天美味(2) - 字符遍历的艺术  

Python 天天美味(3) - 字符转换  

Python 天天美味(4) - isinstance判断对象类型 

Python 天天美味(5) - ljust rjust center  

...

posted @ 2008-05-02 16:47  CoderZh  阅读(4770)  评论(1编辑  收藏  举报