新浪博客

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

2017-12-31 11:40阅读:
问题: Python 2.x 与Python 3.x存在版本兼容问题。Python的新特性不支持向下兼容,导致Python 2.x开发的程序不能在Python 3.x的环境下运行。
思路:把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
** 代码中一定要先引入__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 <wbr>2.x <wbr>与Python <wbr>3.x的版本兼容问题和__future__模块


我的更多文章

下载客户端阅读体验更佳

APP专享