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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值