Help on class basestring in module __builtin__:


class basestring(object)

 |  Type basestring cannot be instantiated; it is the base for str and unicode.

 |  

 |  Data and other attributes defined here:

 |  

 |  __new__ =

 |      T.__new__(S, ...) -> a new object with type S, a subtype of T


basestring()

This abstract type is the superclass for str and unicode. It cannot be called or instantiated, but it can be used to test whether an object is an instance of str or unicode. isinstance(obj, basestring) is equivalent to isinstance(obj, (str, unicode)).


说明:basestring是str和unicode的超类(父类),也是抽象类,因此不能被调用和实例化,但可以被用来判断一个对象是否为str或者unicode的实例,isinstance(obj, basestring)等价于isinstance(obj, (str, unicode));


注意:python3中舍弃了该函数,所以该函数不能在python3中使用。


>>> isinstance("test python",str)

True

>>> isinstance("test python",basestring)

True

>>> isinstance(u"你好",unicode)

True

>>> isinstance(u"你好",basestring)

True


要检查某对象是否为字符串或 Unicode 对象,简单快速的办法是使用内建的 isinstance 和 basestring

>>> def isAString(obj):

...     return isinstance(obj, basestring)

... 

>>> print isAString('abc')

True

>>> print isAString(u'您好')

True

>>> print isAString(list())

False