python2 python3区别
Python开发团队将在2020年1月1日停止对Python2.7的技术支持,但python2的库仍然比较强大(在 pip 官方下载源 pypi 搜索 Python2.7 和 Python3.5 的第三方工具包数可以发现,Python2.7版本对应的第三方工具类目数量是 28523,Python3.5 版本的数量是 12457,这两个版本在第三方工具包支持数量差距相当大 ),因此这里做区别的梳理与介绍:
1实践:
(1)检查python2代码是否可以被Python3编译通过的命令: python3 -m py_compile FILENAME
2理论:
(1)Python3 对 Unicode 字符的原生支持。
Python2 中使用 ASCII 码作为默认编码方式导致 string 有两种类型 str 和 unicode,Python3 只支持 unicode 的 string。
|
Python2 |
Python3 |
|
str类型为非unicode字符串 unicode类型为unicode字符串 |
只有str类型,取消unicode类型 |
|
str和unicode类型继承于basestring |
无basestring类型,str继承于object |
|
不区分str和bytes,两者等价, python2中的str相当于python3中的bytes |
严格区分bytes和str,不允许bytes和str类型的隐式转换 |
Python3严格区分bytes和str,打开二进制文件时一定要指定二进制打开模式 open(FILENAME, "rb")
(2)Python3修改了一些标准模块的名字,按照PEP8的规范,所有module的名字不含大写字母
|
Python2 |
Python3 |
Python2 |
Python3 |
Python2 |
||
|
__builtin__ |
builtins |
dummy_thread |
_dummy_thread |
Tix |
||
|
_winreg |
winreg |
FileDialog |
tkinter.filedialog |
tkColorChooser |
||
|
anydbm |
dbm |
gdbm |
dbm.gnu |
tkCommonDialog |
||
|
BaseHTTPServer |
http.server |
htmlentitydefs |
html.entities |
Tkconstants |
||
|
CGIHTTPServer |
http.server |
HTMLParser |
html.parser |
Tkdnd |
||
|
commands |
subprocess |
httplib |
http.client |
tkFileDialog |
||
|
ConfigParser |
configparser |
markupbase |
_markupbase |
tkFont |
||
|
Cookie |
http.cookies |
Queue |
queue |
Tkinter |
||
|
cookielib |
http.cookiejar |
repr |
reprlib |
tkMessageBox |
||
|
copy_reg |
copyreg |
robotparser |
urllib.robotparser |
tkSimpleDialog |
||
|
cPickle |
pickle |
ScrolledText |
tkinter.scrolledtext |
ttk |
||
|
cStringIO |
io |
SimpleDialog |
tkinter.simpledialog |
urlparse |
||
|
dbhash |
dbm.bsd |
SimpleHTTPServer |
http.server |
UserList |
||
|
dbm |
dbm.ndbm |
SimpleXMLRPCServer |
xmlrpc.server |
UserString |
||
|
Dialog |
tkinter.dialog |
SocketServer |
socketserver |
whichdb |
||
|
DocXMLRPCServer |
xmlrpc.server |
StringIO |
io |
xmlrpclib |
||
|
dumbdbm |
dbm.dumb |
thread |
_thread |
(3)Python3 采用的是绝对路径的方式进行 import ,Python2 中存在老式类和新式类的区别,Python3 统一采用新式类。新式类声明要求继承 object,必须用新式类应用多重继承。
Python3 使用更加严格的缩进。Python2 的缩进机制中,1 个 tab 和 8 个 space 是等价的,所以在缩进中可以同时允许 tab 和 space 在代码中共存。这种等价机制会导致部分 IDE 使用存在问题。Python3 中 1 个 tab 只能找另外一个 tab 替代,因此 tab 和 space 共存会导致报错:TabError:inconsistent use of tabs and spaces in indentation.
|
Python2 |
Python3 |
|
编译时认为8空格等价于1个Tab |
严格缩进,Tab必须和Tab匹配,空格必须和空格匹配 |
(4)
1.print 语句被 Python3 废弃,统一使用 print 函数
2. exec 语句被 python3 废弃,统一使用 exec 函数
3. execfile 语句被 Python3 废弃,推荐使用 exec(open("./filename").read())
4. 不相等操作符"<>"被 Python3 废弃,统一使用"!="
5. long 整数类型被 Python3 废弃,统一使用 int
6. xrange 函数被 Python3 废弃,统一使用 range,Python3 中 range 的机制也进行修改并提高了大数据集生成效率
7. Python3 中这些方法再不再返回 list 对象:dictionary 关联的 keys()、values()、items(),zip(),map(),filter(),但是可以通过 list 强行转换。
8. 迭代器 iterator 的 next()函数被 Python3 废弃,统一使用 next(iterator)
9. raw_input 函数被 Python3 废弃,统一使用 input 函数
10. 字典变量的 has_key 函数被 Python 废弃,统一使用 in 关键词
11. file 函数被 Python3 废弃,统一使用 open 来处理文件,可以通过 io.IOBase 检查文件类型
12. apply 函数被 Python3 废弃
13. 异常 StandardError 被 Python3 废弃,统一使用 Exception
(5)Python2,round 函数返回 float 类型值 ;Python3,round 函数返回 int 类型值
(6)迭代器
|
Python2 |
Python3 |
|
xrange返回迭代器 |
无此函数 |
|
range返回列表 |
range返回迭代器 |
|
map返回列表 |
map返回迭代器 |
|
filter返回列表 |
filter返回迭代器 |
|
zip返回列表 |
zip返回迭代器 |
|
imap/ifilter/izip 返回迭代器 |
这些函数不存在 |
- python3下找不到xrange函数
- 变量类型由列表变为迭代器所导致的问题
建议兼容写法:
- 不使用xrange,一律使用range
- 当需要把range/map/filter/zip的值赋给一个变量,或者作为函数的返回值时,建议使用list()转换为列表
python2 python3区别的更多相关文章
- python2 python3区别(续)
1.除法 Python2 Python3 int/int → int int/int → float python2下整数除以整数返回整数类型,python3下整数除以整数返回浮点数类型 当某些语句假 ...
- Python之路Python3【第零篇】Python2 & Python3区别持续更新~
print def print(self, *args, sep=' ', end='\n', file=None): # known special case of print "&quo ...
- 1. Python2 ,Python3区别
Python2: 1. 源码都含有PHP,Java,C等语言的规范陋习. 2.重复代码比较多. Python3: 源码很规范,清晰,简单,符合Python的宗旨.
- python学习日记(python2/3区别补充,is / id/ encode str,bytes)
python2和python3区别 print python2中,print 是语句 :用法 ---->print '***' python3中,print 是函数:用法----->pri ...
- 同时安装 Python2 & Python3 cmd下版本自由选择
系统:win7 python2.7,python3.6同时安装,于是问题来了,python27与python36文件夹下的文件名都是python.exe 这样在cmd下,直接输入python,自动执行 ...
- python2&python3
1.Python3 使用 print 必须要以小括号包裹打印内容,比如 print('hi') Python2 既可以使用带小括号的方式,也可以使用一个空格来分隔打印内容,比如 print 'hi ...
- Ubuntu安装Python2+Python3
sudo apt-get install python2.7 python2.7-dev sudo apt-get install python3 命令: python 默认执行python2 pyt ...
- Ubuntu16.04 下python2 | python3
在终端分别输入python,python2,python3 python和python2默认都是python2 python3才是python3 Ubuntu下是默认没有pip的,需要自己手动安装 s ...
- windows和linux下 Python2,Python3 的环境及安装
目录 windows和linux下 Python2,Python3 的环境及安装 window下安装 一. 手动安装 二. pip安装 linux下 安装 更新Python 笔者有话 windows和 ...
随机推荐
- ELK日志分析平台搭建
ELK平台介绍 在搜索ELK资料的时候,发现这篇文章比较好,于是摘抄一小段: 以下内容来自:http://baidu.blog.51cto.com/71938/1676798 日志主要包括系统日志.应 ...
- antd-react-mobile(踩坑记录)
1.按照官网步骤进行, $ npm install -g create-react-app # 注意:工具会自动初始化一个脚手架并安装 React 项目的各种必要依赖,如果在过程中出现网络问题,请尝试 ...
- mysql 开启root外部链接权限
mysql给root开启远程访问权限,修改root密码 1.MySql-Server 出于安全方面考虑只允许本机(localhost, 127.0.0.1)来连接访问. 这对于 Web-Server ...
- Max answer(单调栈+ST表)
Max answer https://nanti.jisuanke.com/t/38228 Alice has a magic array. She suggests that the value o ...
- 新学python画一个爱心
from turtle import * def curvemove(): for i in range(200): right(1) forward(1) color('yellow','red') ...
- python的re模块详解
一.正则表达式的特殊字符介绍 正则表达式 ^ 匹配行首 $ 匹配行尾 . 任意单个字符 [] 匹配包含在中括号中的任意字符 [^] 匹配包含在中括号中的字符之外的字符 [-] 匹配指定范围的任意单个字 ...
- jsp请求java返回pdf、excel与word
1,返回pdf关键代码 /** * @todo * @param * @date 2019年3月8日 * @author yanan */ @RequestMapping("/getPdf& ...
- pycharm 有些模块没有提示 解决方法
解决方法:右键模块所在文件夹,选择make directory as ,选择excluded或者sources root,即可.
- Image 图片
随机矩阵画图 这一节我们讲解怎样在matplotlib中打印出图像.这里打印出的是纯粹的数字,而非自然图像.下面用 3x3 的 2D-array 来表示点的颜色,每一个点就是一个pixel. impo ...
- 网址导航18C
[名站] 百度 网易 腾讯 新华 中新 凤凰 [新闻] 联合早报 南方周末 澎湃新闻 [系统] 宋永志 蒲公英 技术员 装机网 系统之家 [软件] 星愿浏览器 微PE [分享] zd423 殁飘遥 ...