使用__future__模块

接下来可能要尝试将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
复制代码

结语 这旧版本中做一些新版特性检测,可以确保我们可以顺利过渡到新版本。 更简单的方法是直接丢到新版本环境去运行,报错了慢慢改过来。

转载于:https://juejin.im/post/5c50071e6fb9a049ee80dbdf

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值