在python老版本中使用新版本的特性

通过__future__模块来实现。

例如,在python2.0中整数除法运算(/)得到的仍然是整数,在python3.0中则改进为'/'得到的是浮点数,'//'得到的是整数。

Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 10/3
3
>>> 10//3
3

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 10/3
3.3333333333333335
>>> 10//3
3

如果想要实现在python2.0中让‘/’实现像python3.0一样得到浮点数的这一新特性。就可以使用__future__模块。

Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import division
>>> 10/3
3.3333333333333335    # 和python3.0一样,得到的是一个浮点数
>>> 10//3
3

猜你喜欢

转载自blog.csdn.net/loner_fang/article/details/80872468