Python里面常用的字符串类型有str和unicode,如果要判断一个对象的类型,最好用basestring,否则可能会判断错误:
1 str1 = "hello" 2 str2 = u"hello" 3 4 print isinstance(str1, str) 5 print isinstance(str1, unicode) 6 print isinstance(str1, basestring) 7 8 print isinstance(str1, str) 9 print isinstance(str1, unicode) 10 print isinstance(str1, basestring)
执行结果: