python2 python3區別

Python開發團隊將在2020年1月1日中止對Python2.7的技術支持,但python2的庫仍然比較強大(在 pip 官方下載源 pypi 搜索 Python2.7 和 Python3.5 的第三方工具包數能夠發現,Python2.7版本對應的第三方工具類目數量是 28523,Python3.5 版本的數量是 12457,這兩個版本在第三方工具包支持數量差距至關大 ),所以這裏作區別的梳理與介紹:html

1實踐:python

(1)檢查python2代碼是否能夠被Python3編譯經過的命令: python3 -m py_compile FILENAMEcookie

2理論:app

(1)Python3 對 Unicode 字符的原生支持。
Python2 中使用 ASCII 碼做爲默認編碼方式致使 string 有兩種類型 str 和 unicode,Python3 只支持 unicode 的 string。
socket

Python2函數

Python3工具

str類型爲非unicode字符串大數據

unicode類型爲unicode字符串ui

只有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()轉換爲列表
相關文章
相關標籤/搜索