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和 ...
随机推荐
- webapi+ajax跨域问题及cookie设置
最近小玩了点东西,发现简单的东西总能遇到点问题 1.webapi跨域设置 [EnableCors(origins: "*", headers: "*", met ...
- SpringCloud-day04-Eureka高可用集群配置
5.4Eureka高可用集群配置 在高并发的情况下一个注册中心难以满足,因此一般需要集群配置多台. 我们再新建两个module microservice-eureka-server-2002, m ...
- 布署配置管理中心apollo
Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性,适用于微服务配置管理场景. 服 ...
- Node.js 薄荷网爬取
Node.js:是一个基于前端的服务器,主要的特点:单线程,异步I/O(对这个没有了解,开发起来真的会踩很多坑),事件驱动 前言:本人主要是一个以使用.Net平台下的语言,进行开发的一个菜鸡,之前面试 ...
- Linux-01
Linux各目录的作用 /bin/ 存放系统命令的目录,普通用户和超级用户都可以执行.不过放在/bin下的命令在单用户模式下也可以执行 /sbin/ 保存和系统环境设置相关的命令,只有超级用户可以使用 ...
- mysql学习笔记--数据库多表查询
一.内连接[inner join] 1. 语法一:select 列名 from 表1 inner join 表2 on 表1.公共字段=表2.公共字段 2. 语法二:select 列名 from 表1 ...
- 安装sklearn_简练解决
安装sklearn_简练解决 < 关键步骤标黑 > 第一步:更新pip python -m pip install --upgrade pip 第二步:安装 scipy 在网址http: ...
- tomcat启动慢
securerandom.source=file:/dev/./urandom 搜索 /""
- fROM PPV report
Month search help: include rmcs0f0m. s_month for s001-spmon at selection-screen on value-request for ...
- 使用idea的springboot项目出现org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
参考: https://www.cnblogs.com/lfm601508022/p/InvalidBoundStatement.html https://blog.csdn.net/xsggsx/a ...