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:

CODE:

import six
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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章