Python天天美味(4)
Python中判度对象类型方法非常简单,不需要像别的语言一样使用如下的判断方法: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
Python天天美味系列(总)
Python 天天美味(1) - 交换变量
Python 天天美味(2) - 字符遍历的艺术
Python 天天美味(3) - 字符转换
Python 天天美味(4) - isinstance判断对象类型
Python 天天美味(5) - ljust rjust center
...
页:
[1]