Home >Backend Development >Python Tutorial >python function - basestring()
basestring()
Explanation: basestring is the super class (parent class) of str and unicode. It is also an abstract class, so it cannot be called and instantiated, but it 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 was introduced after python2.3 and is compatible with all python2 versions after python2.3. Note: This function was abandoned 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
Let's use a practical
to check whether an object is String or Unicode object, the simple and fast way is to use the built-in isinstance and basestring. The usage is as follows:
def isAString(anobj):
return isinstance(anobj, basestring)
The Functions are quite useful, but you must pay attention to its version requirements