Six提供了Python 2和Python 3的相容庫
地址:http://packages.python.org/six/
Six provides simple utilities for wrapping over differences between Python 2 and Python 3.
Six can be downloaded on PyPi. Its bug tracker and code hosting is on BitBucket.
The name, “six”, comes from the fact that 2*3 equals 6. Why not addition? Multiplication is more powerful, and, anyway, “five” has already been snatched away.
Indices and tables
Index
Search Page
Package contents
six.PY3
A boolean indicating if the code is running on Python 3.
Constants
Six provides constants that may differ between Python versions. Ones ending _types are mostly useful as the second argument to isinstance or issubclass.
six.class_types
Possible class types. In Python 2, this encompasses old-style. and new-style. classes. In Python 3, this is just new-styles.
six.integer_types
Possible integer types. In Python 2, this is long() and int(), and in Python 3, just int().
six.string_types
Possible types for text data. This is basestring() in Python 2 and str() in Python 3.
six.text_type
Type for representing (Unicode) textual data. This is unicode() in Python 2 and str() in Python 3.
six.binary_type
Type for representing binary data. This is str() in Python 2 and bytes() in Python 3.
six.MAXSIZE
The maximum size of a container. This is equivalent to sys.maxsize in Python 2.6 and later (including 3.x). Note, this is temptingly similar to, but not the same as sys.maxint in Python 2. There is no direct equivalent to sys.maxint in Python 3 because its integer type has no limits aside from memory.
Here’s example usage of the module:
def dispatch_types(value):
if isinstance(value, six.integer_types):
handle_integer(value)
elif isinstance(value, six.class_types):
handle_class(value)
elif isinstance(value, six.string_types):
handle_string(value)
Six provides simple utilities for wrapping over differences between Python 2 and Python 3.
Six can be downloaded on PyPi. Its bug tracker and code hosting is on BitBucket.
The name, “six”, comes from the fact that 2*3 equals 6. Why not addition? Multiplication is more powerful, and, anyway, “five” has already been snatched away.
Indices and tables
Index
Search Page
Package contents
six.PY3
A boolean indicating if the code is running on Python 3.
Constants
Six provides constants that may differ between Python versions. Ones ending _types are mostly useful as the second argument to isinstance or issubclass.
six.class_types
Possible class types. In Python 2, this encompasses old-style. and new-style. classes. In Python 3, this is just new-styles.
six.integer_types
Possible integer types. In Python 2, this is long() and int(), and in Python 3, just int().
six.string_types
Possible types for text data. This is basestring() in Python 2 and str() in Python 3.
six.text_type
Type for representing (Unicode) textual data. This is unicode() in Python 2 and str() in Python 3.
six.binary_type
Type for representing binary data. This is str() in Python 2 and bytes() in Python 3.
six.MAXSIZE
The maximum size of a container. This is equivalent to sys.maxsize in Python 2.6 and later (including 3.x). Note, this is temptingly similar to, but not the same as sys.maxint in Python 2. There is no direct equivalent to sys.maxint in Python 3 because its integer type has no limits aside from memory.
Here’s example usage of the module:
CODE:
import sixdef dispatch_types(value):
if isinstance(value, six.integer_types):
handle_integer(value)
elif isinstance(value, six.class_types):
handle_class(value)
elif isinstance(value, six.string_types):
handle_string(value)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-741565/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- windows下 相容Python2和Python3WindowsPython
- Python2 和 Python3 的區別及相容技巧Python
- 讓你的python程式同時相容python2和python3Python
- pickle 在python 2和python 3中相容性問題Python
- 編寫相容 Python 2.x 和 3.x 程式碼的方法Python
- python2和python3的不同Python
- Python2和Python3的區別Python
- Python開發:Python2和Python3的共存和切換使用Python
- urllib庫在python2和python3環境下的使用區別Python
- python3和python2中的filter區別PythonFilter
- Python2和Python3中print的不同點Python
- python3 和 python2 input 功能的不同點Python
- Python2和Python3的10大區別Python
- 詳解python2 和 python3的區別Python
- Python3.x和Python2.x的區別Python
- Python2和Python3的區別?Python入門學習Python
- range方法在Python2和Python3中的不同Python
- 解釋 Python 2 和 Python 3 的版本之間差別Python
- python2.x和python3.x區別Python
- 完美解決 Python2 和 python3 共存Python
- Python 2 和 Python 3 主要區別有哪些(一)?Python
- python 標準庫和第3方庫的介紹Python
- 示例解讀 Python 2 和 Python 3 之間的主要差異Python
- Python3和Python2 異常處理except的不同Python
- 當同時安裝Python2和Python3後,如何相容並切換使用詳解(比如pip使用)Python
- Mac安裝python2和python3,並配置MacPython
- Python 2 和 Python 3 主要區別有哪些(一)薦Python
- 除法運算子/在Python2和Python3中的區別Python
- python2和3的一些區別Python
- Mac OS下 Anaconda Python2 和 Python3 配置MacPython
- Anaconda快捷搭建Python2和Python3環境Python
- Python2與Python3的區別Python
- Google開源了Abseil,為C++和Python開發提供支援GoC++Python
- Python升級和相容性配置Python
- Ubuntu下同時安裝了python2版本和python3版本如何使用pipUbuntuPython
- python2到python3程式碼轉化:2to3Python
- 學Python用哪個版本?Python3和Python2有何區別?Python
- 使用 Vue Demi 構建同時相容Vue2和Vue3的元件庫Vue元件