six Python modules,

background

We all know that there are now two large python version, another one is python2 is python3, so different people may be accustomed to different versions, while python2 and python3 there are some differences and incompatibilities, to create a program ape a lot of trouble, how to do it?

effect

Six is ​​to solve this trouble, which is a module designed to be compatible Python 2 and Python 3, which solves part of such a method is incompatible urllib, str and bytes types are not compatible with other "well-known" problem.

use

import six

six.PY2 #返回一个表示当前运行环境是否为python2的boolean值
six.PY3 #返回一个表示当前运行环境是否为python3的boolean值

six.integer_types # 在python2中,存在 int 和 long 两种整数类型;在python3中,仅存在一种类型int
six.string_types # 在python2中,使用的为basestring;在python3中,使用的为str
six.text_type # 在python2中,使用的文本字符的类型为unicode;在python3中使用的文本字符的类型为str
six.binary_type # 在python2中,使用的字节序列的类型为str;在python3中使用的字节序列的类型为bytes

six functions and many, not commonly used here is not listed, you can consult the documentation when needed.

Guess you like

Origin www.cnblogs.com/mrdoghead/p/12004030.html