接下来可能要尝试将python2.x的代码升级为python3.x。 这里学习了一些解决冲突的方法。
冲突点:
比如2.x里的字符串用'xxx'表示str, Unicode字符串用u'xxx'表示Unicode,而在3.x中,所有的字符串都是Unicode,因此,写u'xxx'和'xxx'是完全一致的。而在2.x中以'xxx'表示的str必须写成b'xxx',用来表示二进制字符串。
python提供__future__模块。可以用于在当前版本中测试 一些新版本的特性。
为了适应Python 3.x的新的字符串的表示方法,在2.7版本的代码中,可以通过unicode_literals来使用Python 3.x的新的语法:
# still running on Python 2.7
from __future__ import unicode_literals
print '\'xxx\' is unicode?', isinstance('xxx', unicode)
print 'u\'xxx\' is unicode?', isinstance(u'xxx', unicode)
print '\'xxx\' is str?', isinstance('xxx', str)
print 'b\'xxx\' is str?', isinstance(b'xxx', str)
复制代码
注意到上面的代码仍然在Python 2.7下运行,但结果显示去掉前缀u的'a string'仍是一个unicode,而加上前缀b的b'a string'才变成了str:
$ python task.py
'xxx' is unicode? True
u'xxx' is unicode? True
'xxx' is str? False
b'xxx' is str? True
复制代码
类似的情况还有除法运算。在Python 2.x中,对于除法有两种情况,如果是整数相除,结果仍是整数,余数会被扔掉,这种除法叫“地板除”:
>>> 10 / 3
3
复制代码
要做精确除法,必须把其中一个数变成浮点数:
>>> 10.0 / 3
3.3333333333333335
复制代码
而在Python 3.x中,所有的除法都是精确除法,地板除用//表示:
$ python3
Python 3.3.2 (default, Jan 22 2014, 09:54:40)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 10 / 3
3.3333333333333335
>>> 10 // 3
3
复制代码
如果你想在Python 2.7的代码中直接使用Python 3.x的除法,可以通过__future__模块的division实现:
from __future__ import division
print '10 / 3 =', 10 / 3
print '10.0 / 3 =', 10.0 / 3
print '10 // 3 =', 10 // 3
复制代码
结果如下:
10 / 3 = 3.33333333333
10.0 / 3 = 3.33333333333
10 // 3 = 3
复制代码
结语 这旧版本中做一些新版特性检测,可以确保我们可以顺利过渡到新版本。 更简单的方法是直接丢到新版本环境去运行,报错了慢慢改过来。