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()转换为列表
posted on 2019-02-14 16:46  berrycake  阅读(339)  评论(0编辑  收藏  举报