有什么区别:
isinstance(foo, types.StringType)
Run Code Online (Sandbox Code Playgroud)
和
isinstance(foo, basestring)
Run Code Online (Sandbox Code Playgroud)
?
Luk*_*ský 18
对于Python2:basestring是两个基类str和unicode,而types.StringType 为 str.如果要检查某些内容是否为字符串,请使用basestring.如果你想检查某些东西是否为字节串,请使用str并忘记types.
Joh*_*ooy 10
这个东西在Python3中完全不同
types不再StringType
str是unicode总是
basestring不复存在
因此,如果您可能需要移植代码,请尽量不要在代码中撒些太多东西