Python 2.x 与Python 3.x的版本兼容问题和__future__模块

标签:
__future__python兼容问题新功能杂谈 |
分类: Python |
问题: Python 2.x 与Python 3.x存在版本兼容问题。Python的新特性不支持向下兼容,导致Python
2.x开发的程序不能在Python 3.x的环境下运行。
** 代码中一定要先引入__future__模块,再引入其它模块。A future
statement must appear near the top of the module. The only lines
that can appear before a future statement are: the module docstring
(if any), comments, blank lines, and other future statements.
思路:把Python的新功能引入到当前项目中,使目前的代码能够在当前的Python版本下,兼容Python的新功能,为向Python新版本的过度提供准备。
方法:Python提供了__future__模块,实现将Python 3.x的新功能引入到Python
2.x代码中,为向Python 3.x的过度提供准备。
代码:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
http://s14/mw690/001qpLv2zy7h0pG46Jfed&6902.x 与Python 3.x的版本兼容问题和__future__模块" TITLE="Python 2.x 与Python 3.x的版本兼容问题和__future__模块" />
前一篇:DPDK的效率优化