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和 ...
随机推荐
- 1_translation_1
It is always difficult to start describing a programming language because little details do not make ...
- sql优化之concat/concat_ws/group_concat
原文1:https://baijiahao.baidu.com/s?id=1595349117525189591&wfr=spider&for=pc 原文2:https://www.y ...
- program_options
[program_options] The program_options library allows program developers to obtain program options, t ...
- Masonry与AmazeUI结合实现瀑布流
做一个图片列表展示,由于照片数量太多,决定用瀑布流来实现 由于之前没有接触过瀑布流,不知从何下手 百度一下大家都在用Masonry 官网 https://masonry.desandro.com/ 这 ...
- redis 开机启动
使用chkconfig开机启动redis. 把redis初始脚本拷贝到/etc/init.d/下面. #cd /usr/local/redis/redis-2.6.16/utils # cp redi ...
- 【练习】Python第三次
对函数,内置函数的用法,递归,程序运行顺序的考试 1.列举布尔值为 False 的值 0 False '' [] () {} None 经常使用的场景是 if object 如果object有值就执行 ...
- CodeWarrior 10 配置Jlint初始化文件
新建一个项目之后,飞思卡尔的仿真器的配置不如德州仪器那么简单.他需要一些配置. 当我们新建一个工程后,可以采取如下步骤配置Jlint: 1.右击工程名,选择属性. 2.在Run/Debug Setti ...
- 初学Python:面向对象总结
2019-04-16 Python 3x 一. 面向对象的含义和特性 面向对象是将世界中的具体事物进行抽象,从而能够更好的帮助我们归纳总结,解决复杂问题的一种解决问题的思路. 面向对象的三个特性——封 ...
- js动态添加元素绑定事件问题
//开始是直接普通写的绑定click事件 其中li a i 中的i是动态添加的 结果是触发不了..$("li a i").click(function () { $(this).m ...
- Mybatis之批量操作
首先批量操作的优点是:大大的提高查询的效率. 举个简单的例子:如果在程序中遍历来执行sql的话,这种情况就是有多少行数据就要执行多少条sql,这样导致的效率将是非常低. 如下可能需要40s inser ...