Python function-basestring ()

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.