Python function-basestring ()

Source: Internet
Author: User
Basestring ()

Description: Basestring is a superclass of STR and Unicode (parent class), which is also an abstract class, and therefore cannot be called and instantiated, but can be used to determine whether an object is a str or an instance of Unicode, isinstance (obj, basestring) Equivalent to Isinstance (obj, (str, Unicode));

Version: python2.3 version later introduced this function, compatible with python2.3 later python2 versions. Note: This function is discarded in python3, so the 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


Here's a practical one.


A quick and easy way to check whether an object is a string or Unicode object is to use the built-in isinstance and basestring, as follows:


def isastring (anobj):

Return Isinstance (Anobj, basestring)


This function is still useful, but be aware of 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.