![]() | 1 ruoyu0088 2015-01-29 14:13:56 +08:00 你的程序和你的问题对不上啊。 |
![]() | 2 raquelken 2015-01-29 14:17:23 +08:00 python里面判断类型的方法是 isinstance |
![]() | 3 broono 2015-01-29 14:24:13 +08:00 不应该是这样吗: if isinstance(dataA,'str') and isinstance(dataB,'str'): print 'True' |
![]() | 5 hush OP 上面的两个方法都能运行得到效果,但就第二种不全面 |
![]() | 6 hahastudio 2015-01-29 14:35:36 +08:00 你的问题大概是你觉得找到了 >>> s = "a" >>> isinstance(s, str) True >>> type(s) == str True >>> class bar(str): def __init__(self): pass >>> b = bar() >>> type(b) == str False >>> isinstance(b, str) True 然后你要知道 x.__str__() <==> str(x) 就可以了 str(x) 就相当于其他语言的 x.ToString() |
![]() | 7 hush OP @hahastudio 你的意思是 str(X)使用这个是不会有错的吗? |
![]() | 10 Valyrian 2015-01-29 15:24:08 +08:00 方法一是错的,左边是str右边是type 方法二也是错的,如果dataA类型的__eq__()方法允许和str相等的话 建议isinstance(dataA, str)或者type(dataA) == type("") |
![]() | 11 Valyrian 2015-01-29 15:28:40 +08:00 补充一句,如果dataA是一个str的子类,isinstance()会true,type(dataA) == type("")会false 所以你如果想要dataA是严格的str就用type(dataA) == type(""),如果不在乎是子类就用isinstance() |
![]() | 12 zhicheng 2015-01-29 18:44:57 +08:00 >>> isinstance('abc', str) True >>> isinstance(u'abc', str) False >>> isinstance('abc', unicode) False >>> isinstance(u'abc', unicode) True >>> isinstance('abc', basestring) True >>> isinstance(u'abc', basestring) True |