Python function-basestring ()
Note: basestring is a super class (parent class) of str and unicode, and is also an abstract class. Therefore, it cannot be called or instantiated, but can be used to determine whether an object is an instance of str or unicode, isinstance (obj, basestring) is equivalent to isinstance (obj, (str, unicode ));
Version: This function will be introduced later in python2.3 and compatible with versions later than python2.3. Note: This function is discarded in python3, so this function cannot be used in python3.
Example:
>>> Isinstance ("Hello world", str)
True
>>> Isinstance ("Hello world", basestring)
True
>>> Isinstance (u "Hello", unicode)
True
>>> Isinstance (u "Hello", basestring)
True
A practical
To check whether an object is a string or Unicode object, you can use the built-in isinstance and basestring as follows:
Def isAString (anobj ):
Return isinstance (anobj, basestring)
This function is useful, but you must pay attention to its version requirements.