isinstance(’aaa’,basestring)和isinstance(’aaa’,str)有什么区别?

问题:isinstance(’aaa’,basestring)和isinstance(’aaa’,str)有什么区别?

a='aaaa'
print isinstance(a, basestring)#true
print isinstance(a, str)#true
a='aaaa'
print isinstance(a, basestring)#true
print isinstance(a, str)#true

回答 0

在3.0之前的Python版本中,有两种字符串:“普通字符串”和“ unicode字符串”。纯字符串(str)不能表示拉丁字母之外的字符(为简单起见,忽略代码页的详细信息)。Unicode字符串(unicode)可以代表任何字母的字符,包括虚构的字母,例如Klingon。

那么,为什么要使用两种字符串,仅使用Unicode会更好,因为这将涵盖所有情况?最好只使用Unicode,但是在Unicode是表示字符串的首选方法之前创建了Python。在具有许多用户的语言中转换字符串类型需要花费时间,在Python 3.0中,最终所有字符串都是Unicode。

3.0版之前的Python字符串的继承层次结构为:

          object
             |
             |
         basestring
            / \
           /   \
         str  unicode

可以将Python 2.3中引入的“ basestring”视为朝着字符串统一方向迈出的一步,因为它可用于检查对象是否为str或的实例。unicode

>>> string1 = "I am a plain string"
>>> string2 = u"I am a unicode string"
>>> isinstance(string1, str)
True
>>> isinstance(string2, str)
False
>>> isinstance(string1, unicode)
False
>>> isinstance(string2, unicode)
True
>>> isinstance(string1, basestring)
True
>>> isinstance(string2, basestring)
True

In Python versions prior to 3.0 there are two kinds of strings “plain strings” and “unicode strings”. Plain strings (str) cannot represent characters outside of the Latin alphabet (ignoring details of code pages for simplicity). Unicode strings (unicode) can represent characters from any alphabet including some fictional ones like Klingon.

So why have two kinds of strings, would it not be better to just have Unicode since that would cover all the cases? Well it is better to have only Unicode but Python was created before Unicode was the preferred method for representing strings. It takes time to transition the string type in a language with many users, in Python 3.0 it is finally the case that all strings are Unicode.

The inheritance hierarchy of Python strings pre-3.0 is:

          object
             |
             |
         basestring
            / \
           /   \
         str  unicode

‘basestring’ introduced in Python 2.3 can be thought of as a step in the direction of string unification as it can be used to check whether an object is an instance of str or unicode

>>> string1 = "I am a plain string"
>>> string2 = u"I am a unicode string"
>>> isinstance(string1, str)
True
>>> isinstance(string2, str)
False
>>> isinstance(string1, unicode)
False
>>> isinstance(string2, unicode)
True
>>> isinstance(string1, basestring)
True
>>> isinstance(string2, basestring)
True

回答 1

所有字符串都是基本字符串,但是unicode字符串不是str类型。尝试以下方法:

>>> a=u'aaaa'
>>> print isinstance(a, basestring)
True
>>> print isinstance(a, str)
False

All strings are basestrings, but unicode strings are not of type str. Try this instead:

>>> a=u'aaaa'
>>> print isinstance(a, basestring)
True
>>> print isinstance(a, str)
False

回答 2

您真正要问的是basestring和str类之间的区别。

Str是从basestr继承的类。但是,如果您要创建unicode字符串,那么它也和其他字符串一样存在。

>>> a = u'aaaa'
>>> isinstance(a, str)
False
>>> isinstance(a, basestring)
True

Really what you’re asking is the difference between the basestring and str class.

Str is a class that inherits from basestr. But unicode strings also exist, as could other ones, if you wanted to make one.

>>> a = u'aaaa'
>>> isinstance(a, str)
False
>>> isinstance(a, basestring)
True

回答 3

Basestring是字符串的超类。在您的示例中,a的类型为“ str”,因此它既是基本字符串,又是str

Basestring is the superclass of string. In your example, a is of type “str” thus, it is both a basestring, and a str