【脚本语言系列】关于Python基础知识兼容Python 2.x+Python 3.x,你需要知道的事
来源:互联网 发布:淘宝怎么打印电子发票 编辑:程序博客网 时间:2024/06/11 01:54
如何兼容Python 2.x+Python 3.x
- 保留字变函数
Python 2.x使用__future__模块使用Python 3.x中的函数
# -*- coding:utf-8 -*-# For Python 2.xprint print
File "<ipython-input-13-8fe752f7a342>", line 3 print print ^SyntaxError: invalid syntax
# -*- coding:utf-8 -*-# For Python 2.x & Python 3.xfrom __future__ import print_functionprint(print)
<built-in function print>
- 模块更名
使用try/except模块处理Python 2.x与Python 3.x不同名的模块
# -*- coding:utf-8 -*-# For Python 2.x & Python 3.xtry: import urllib.request as urllib_request # For Python 3.xexcept ImportError: import urllib2 as urllib_request # For Python 2.x
- 模块/功能过时
使用future模块禁用Python 2.x可用但Python 3.x过时的模块
# -*- coding:utf-8 -*-# For Python 2.x & Python 3.xfrom future.builtins.disabled import *apply()
---------------------------------------------------------------------------NameError Traceback (most recent call last)<ipython-input-15-99a87af09d54> in <module>() 3 from future.builtins.disabled import * 4 ----> 5 apply()c:\python27\lib\site-packages\future\builtins\disabled.pyc in disabled(*args, **kwargs) 55 no longer a builtin in Python 3. 56 '''---> 57 raise NameError('obsolete Python 2 builtin {0} is disabled'.format(name)) 58 return disabled 59 NameError: obsolete Python 2 builtin apply is disabled
什么是兼容Python 2.x+Python 3.x
常用的兼容Python2.x和Python3.x的方式是:
* 开发2个模块,分别针对Python 2.x和针对_ Python 3.x_;
* 使用兼容模块,用于兼容Python 2.x和Python 3.x;
阅读全文
0 0
- 【脚本语言系列】关于Python基础知识兼容Python 2.x+Python 3.x,你需要知道的事
- 【脚本语言系列】关于Python系统管理,你需要知道的事
- 【脚本语言系列】关于Python容器,你需要知道的事
- 【脚本语言系列】关于Python基础知识处理字符串,你需要知道的事
- 【脚本语言系列】关于Python基础知识文件操作,你需要知道的事
- 【脚本语言系列】关于Python基础知识设计模式,你需要知道的事
- 【脚本语言系列】关于Python基础知识面向对象编程,你需要知道的事
- 【脚本语言系列】关于Python基础知识魔法变量,你需要知道的事
- 【脚本语言系列】关于Python基础知识调试,你需要知道的事
- 【脚本语言系列】关于Python基础知识协程,你需要知道的事
- 【脚本语言系列】关于Python基础知识函数缓存,你需要知道的事
- 【脚本语言系列】关于Python基础知识映射器和过滤器,你需要知道的事
- 【脚本语言系列】关于Python基础知识__slots__,你需要知道的事
- 【脚本语言系列】关于Python基础知识Lambda表达式,你需要知道的事
- 【脚本语言系列】关于Python基础知识枚举,你需要知道的事
- 【脚本语言系列】关于Python基础知识对象自省,你需要知道的事
- 【脚本语言系列】关于Python基础知识异常处理,你需要知道的事
- 【脚本语言系列】关于Python基础知识推导式,你需要知道的事
- POJ_3090_Visible Lattice Points
- 遗传算法入门(一)
- Coursera Algorithms week1 Interview Questions: Egg drop
- IntelliJ IDEA快捷键(常用)
- HttpClient简单实用
- 【脚本语言系列】关于Python基础知识兼容Python 2.x+Python 3.x,你需要知道的事
- ThreadPool.QueueUserWorkItem 方法 (WaitCallback)
- 译密码
- 【XC6SLX9 TQC144】50MHz系统时钟分频实现LED跑马灯
- PHP(3)--CI框架的使用
- 不带权的线段覆盖问题
- linux系统用户以及用户组管理
- ubuntu+win10双系统,调整分区大小后进入了emergency mode
- MS SQL Server分析数据库的I/O性能