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

返回迭代器

这些函数不存在

  1. python3下找不到xrange函数
  2. 变量类型由列表变为迭代器所导致的问题

建议兼容写法:

  1. 不使用xrange,一律使用range
  2. 当需要把range/map/filter/zip的值赋给一个变量,或者作为函数的返回值时,建议使用list()转换为列表

python2 python3区别的更多相关文章

  1. python2 python3区别(续)

    1.除法 Python2 Python3 int/int → int int/int → float python2下整数除以整数返回整数类型,python3下整数除以整数返回浮点数类型 当某些语句假 ...

  2. Python之路Python3【第零篇】Python2 & Python3区别持续更新~

    print def print(self, *args, sep=' ', end='\n', file=None): # known special case of print "&quo ...

  3. 1. Python2 ,Python3区别

    Python2: 1. 源码都含有PHP,Java,C等语言的规范陋习. 2.重复代码比较多. Python3: 源码很规范,清晰,简单,符合Python的宗旨.

  4. python学习日记(python2/3区别补充,is / id/ encode str,bytes)

    python2和python3区别 print python2中,print 是语句 :用法 ---->print '***' python3中,print 是函数:用法----->pri ...

  5. 同时安装 Python2 & Python3 cmd下版本自由选择

    系统:win7 python2.7,python3.6同时安装,于是问题来了,python27与python36文件夹下的文件名都是python.exe 这样在cmd下,直接输入python,自动执行 ...

  6. python2&python3

    1.Python3 使用 print 必须要以小括号包裹打印内容,比如 print('hi')   Python2 既可以使用带小括号的方式,也可以使用一个空格来分隔打印内容,比如 print 'hi ...

  7. Ubuntu安装Python2+Python3

    sudo apt-get install python2.7 python2.7-dev sudo apt-get install python3 命令: python 默认执行python2 pyt ...

  8. Ubuntu16.04 下python2 | python3

    在终端分别输入python,python2,python3 python和python2默认都是python2 python3才是python3 Ubuntu下是默认没有pip的,需要自己手动安装 s ...

  9. windows和linux下 Python2,Python3 的环境及安装

    目录 windows和linux下 Python2,Python3 的环境及安装 window下安装 一. 手动安装 二. pip安装 linux下 安装 更新Python 笔者有话 windows和 ...

随机推荐

  1. jar 接收utf-8字符乱码现象

    1.今天用php去调用jar出现乱码现象 需要对传递的中文字符进行编码之后再传递这里我用的是urlencode编码,让后java再解码就正常了 这里贴一下代码: <?phpexec(" ...

  2. java打包发布程序.jar(Eclipse)

    1.程序运行无错误后,右击项目名称,选择Export 2.next后,选择启动时的运行项目,选择压缩包的存储路径 3.finishi即可. 4.运行 cmd存储路径下>java -jar XXX ...

  3. JDK、CGlib动态代理详解

    Java动态代理之JDK实现和CGlib实现(简单易懂)      一 JDK和CGLIB动态代理原理 1.JDK动态代理 利用拦截器(拦截器必须实现InvocationHanlder)加上反射机制生 ...

  4. Odoo domain 中的 like, ilike, =like, =ilike

    Odoo domain 中的 like, ilike, =like, =ilike 举例说明[转]   Odoo domain 中的 like, ilike, =like, =ilike Odoo d ...

  5. 监控服务器配置(四)-----OracleDb_exporter安装配置

    1.下载oracle客户端安装包(linux版)到 /opt/minitor/oracleDb . 下载地址:https://download.csdn.net/download/a155657721 ...

  6. Openssl asn1parse命令

    一.简介 asn1parse命令是一种用来诊断ASN.1结构的工具,也能用于从ASN1.1数据中提取数据 二.语法 openssl asn1parse [-inform PEM|DER] [-in f ...

  7. 11.Redis缓存设计

    11.Redis缓存设计11.1 缓存的收益和成本11.2 缓存更新策略11.3 缓存粒度控制11.4 穿透优化11.5 无底洞优化11.6 雪崩优化11.7 热点key重建优化11.8 本章重点回顾

  8. AST的作用

    ·代码版本兼容:例如babel ·代码混淆和压缩:将语义变量变无意义 ·开发工具:webpack.vue-cli ·编译:编译器.IDE

  9. 洛谷1027 Car的旅行路线

    原题链接 将每个城市拆成四个点,即四个机场来看,那么这题就是求最短路. 不过建图有些麻烦,先要找出第四个机场的坐标. 设另外三个机场的坐标为\((x_1, y_1), (x_2, y_2), (x_3 ...

  10. Linux permission denied问题

    初试Linux 本来想试一试递归的mkdir -p结果遇到了permission denied,但是自己明明是管理员啊 查了博客,在文件名前加上chmod 777 ,例如chmod 777  temp ...