about 3 years ago

Know the Differences Between bytes, str, and unicode

這個條款在講 Python 2 和 3 處理字串的不同。

Python 2

  • str: raw 8-bit values
  • unicode: Unicode
      object
         |
         |
     basestring
        / \
       /   \
     str  unicode
    
    Python 3
  • bytes: raw 8-bit values
  • str: Unicode

Python 3 沒有 basestring

six 程式碼

if PY3:
    string_types = str,
    text_type = str
    binary_type = bytes
else:
    string_types = basestring,
    text_type = unicode
    binary_type = str
← Effective Python 心得筆記: Item 2 Effective Python 心得筆記: Item 4 →
 
comments powered by Disqus