杰瑞科技汇

Python中basestring是什么类型?

Of course! Let's break down basestring in Python.

Python中basestring是什么类型?-图1
(图片来源网络,侵删)

The Short Answer

basestring was an abstract base class in Python 2 that represented all text string types. Its main purpose was to allow you to check if a variable was a string (either str or unicode) without caring which specific type it was.

It does not exist in Python 3.


The Detailed Explanation: Python 2 vs. Python 3

To understand basestring, you have to understand the context of Python 2's handling of strings.

The Problem in Python 2

In Python 2, there were two main string types:

Python中basestring是什么类型?-图2
(图片来源网络,侵删)
  • str: A sequence of bytes. It was the default string type. It was used for ASCII text but couldn't natively handle characters outside the ASCII set (like or ) without errors or special encoding.
  • unicode: A sequence of abstract characters. It was designed to handle text from any language in the world. It was not the default; you had to explicitly create it by prefixing a string with a u (e.g., u"hello").

This created a common problem: what if you were writing a function that should accept any kind of text, whether it was a simple byte-string (str) or a full Unicode string (unicode)? You didn't want to write two separate checks.

# Python 2
def process_text(s):
    if type(s) == str: # This only catches byte-strings
        print "Processing a byte-string"
    elif type(s) == unicode: # This only catches unicode strings
        print "Processing a unicode string"
    else:
        raise TypeError("I need a string, not a " + type(s).__name__)
process_text("hello")      # Works
process_text(u"你好")      # Works
process_text(123)          # Raises TypeError

This was tedious and not very "Pythonic."

The Solution: basestring

Python 2.2 introduced basestring as the common base class for both str and unicode. It was an abstract base class, meaning you couldn't create an instance of it directly, but you could use it for type checking.

This simplified the code significantly.

# Python 2 - The Better Way
def process_text(s):
    # Check if 's' is an instance of either str OR unicode
    if isinstance(s, basestring):
        print "Processing a string (either str or unicode)"
    else:
        raise TypeError("I need a string, not a " + type(s).__name__)
process_text("hello")      # Works
process_text(u"你好")      # Works
process_text(123)          # Raises TypeError

Key takeaway for Python 2: isinstance(s, basestring) was the standard, correct way to check if a variable s held any kind of text string.


What Happened in Python 3? (The Great String Unification)

Python 3 completely redesigned how strings work to solve the confusion of Python 2.

  • str is now Unicode: The default str type in Python 3 is a sequence of Unicode characters, just like Python 2's unicode. It can handle any language natively.
  • bytes is for bytes: A new type, bytes, was introduced to represent raw byte data (like the old Python 2 str).
  • unicode is gone: The unicode type was removed entirely. The str type now handles all text.

Because there is now only one text string type (str), the need for a base class like basestring disappeared.

The Python 3 Equivalent

In Python 3, checking if a variable is a string is much simpler. You just check if it's an instance of str.

# Python 3
def process_text(s):
    # In Python 3, all text is 'str'. We just check for 'str'.
    if isinstance(s, str):
        print "Processing a string (it's always a Unicode str)"
    else:
        raise TypeError("I need a string, not a " + type(s).__name__)
process_text("hello")      # Works (this is a Unicode string)
process_text("你好")      # Works (this is also a Unicode string)
process_text(b"hello")    # This is a 'bytes' object, so it will raise TypeError
process_text(123)          # Raises TypeError

Summary Table

Feature Python 2 Python 3
Text String Type str (bytes) and unicode (characters) Only str (always Unicode)
Byte String Type str (bytes) bytes
Base String Class basestring (abstract base for str and unicode) Does not exist
How to check for a text string? isinstance(s, basestring) isinstance(s, str)

Practical Advice

  • If you are writing code for Python 2 only: Use isinstance(s, basestring).
  • If you are writing code for Python 3 only: Use isinstance(s, str).
  • If you are writing code that must work in both Python 2 and 3: You need a compatibility layer. The best way is to try to import basestring and, if it fails (because you're in Python 3), define it as str.
# Compatibility layer for both Python 2 and 3
try:
    # In Python 2, this will succeed
    basestring
except NameError:
    # In Python 3, basestring doesn't exist, so we define it as str
    basestring = str
# Now you can use isinstance(s, basestring) in both versions
def process_text(s):
    if isinstance(s, basestring):
        print "Processing a string"
    else:
        raise TypeError("I need a string")
分享:
扫描分享到社交APP
上一篇
下一篇