加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

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

(2017-12-31 11:40:20)
标签:

__future__

python

兼容问题

新功能

杂谈

分类: Python
问题: 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.

http://s14/mw690/001qpLv2zy7h0pG46Jfed&6902.x 与Python 3.x的版本兼容问题和__future__模块" TITLE="Python 2.x 与Python 3.x的版本兼容问题和__future__模块" />

  




0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有