Home > Backend Development > Python Tutorial > python function - basestring()

python function - basestring()

高洛峰
Release: 2016-10-17 15:38:08
Original
1284 people have browsed it

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


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template