Python3x和Python2x的區(qū)別_第1頁(yè)
Python3x和Python2x的區(qū)別_第2頁(yè)
Python3x和Python2x的區(qū)別_第3頁(yè)
Python3x和Python2x的區(qū)別_第4頁(yè)
Python3x和Python2x的區(qū)別_第5頁(yè)
已閱讀5頁(yè),還剩6頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、python3.x和python2.x的區(qū)別 這個(gè)星期開(kāi)始學(xué)習(xí)python了,因?yàn)榭吹臅?shū)都是基于python2.x,而且我安裝的是python3.1,所以書(shū)上寫(xiě)的地方好多都不適用于python3.1,特意在google上search了一下3.x和2.x的區(qū)別。特此在自己的空間中記錄一下,以備以后查找方便,也可以分享給想學(xué)習(xí)python的friends.1.性能 py3.0運(yùn)行 pystone benchmark的速度比py2.5慢30%。guido認(rèn)為py3.0有極大的優(yōu)化空間,在字符串和整形操作上可 以取得很好的優(yōu)化結(jié)果。 py3.1性能比py2.5慢15%,還有很大的提升空間。 2.編碼

2、py3.x源碼文件默認(rèn)使用utf-8編碼,這就使得以下代碼是合法的: >>> 中國(guó) = china >>>print(中國(guó)) china 3. 語(yǔ)法 1)去除了<>,全部改用!= 2)去除,全部改用repr() 3)關(guān)鍵詞加入as 和with,還有true,false,none 4)整型除法返回浮點(diǎn)數(shù),要得到整型結(jié)果,請(qǐng)使用/ 5)加入nonlocal語(yǔ)句。使用noclocal x可以直接指派外圍(非全局)變量 6)去除print語(yǔ)句,加入print()函數(shù)實(shí)現(xiàn)相同的功能。同樣的還有 exec語(yǔ)句,已經(jīng)改為exec()函數(shù) 例如: 2.x: pr

3、int the answer is, 2*2 3.x: print(the answer is, 2*2) 2.x: print x, # 使用逗號(hào)結(jié)尾禁止換行 3.x: print(x, end= ) # 使用空格代替換行 2.x: print # 輸出新行 3.x: print() # 輸出新行 2.x: print >>sys.stderr, fatal error 3.x: print(fatal error, file=sys.stderr) 2.x: print (x, y) # 輸出repr(x, y) 3.x: print(x, y) # 不同于print(x, y

4、)! 7)改變了順序操作符的行為,例如x<y,當(dāng)x和y類型不匹配時(shí)拋出typeerror而不是返回隨即的 bool值 8)輸入函數(shù)改變了,刪除了raw_input,用input代替: 2.x:guess = int(raw_input(enter an integer : ) # 讀取鍵盤(pán)輸入的方法 3.x:guess = int(input(enter an integer : ) 9)去除元組參數(shù)解包。不能def(a, (b, c):pass這樣定義函數(shù)了 10)新式的8進(jìn)制字變量,相應(yīng)地修改了oct()函數(shù)。 2.x的方式如下: >>> 0666 438 >

5、>> oct(438) 0666 3.x這樣: >>> 0666 syntaxerror: invalid token (<pyshell#63>, line 1) >>> 0o666 438 >>> oct(438) 0o666 11)增加了 2進(jìn)制字面量和bin()函數(shù) >>> bin(438) 0b110110110 >>> _438 = 0b110110110 >>> _438 0b110110110 12)擴(kuò)展的可迭代解包。在py3.x 里,a, b,

6、*rest = seq和 *rest, a = seq都是合法的,只要求兩點(diǎn):rest是list 對(duì)象和seq是可迭代的。 13)新的super(),可以不再給super()傳參數(shù), >>> class c(object): def _init_(self, a): print(c, a) >>> class d(c): def _init(self, a): super()._init_(a) # 無(wú)參數(shù)調(diào)用super() >>> d(8) c 8 <_main_.d object at 0x00d7ed90> 14)新的me

7、taclass語(yǔ)法: class foo(*bases, *kwds): pass 15)支持class decorator。用法與函數(shù)decorator一樣: >>> def foo(cls_a): def print_func(self): print(hello, world!) cls_a.print = print_func return cls_a >>> foo class c(object): pass >>> c().print() hello, world! class decorator可以用來(lái)玩玩貍貓換太子的大把戲。

8、更多請(qǐng)參閱pep 3129 4. 字符串和字節(jié)串 1)現(xiàn)在字符串只有str一種類型,但它跟2.x版本的unicode幾乎一樣。 2)關(guān)于字節(jié)串,請(qǐng)參閱“數(shù)據(jù)類型”的第2條目 5.數(shù)據(jù)類型 1)py3.x去除了long類型,現(xiàn)在只有一種整型int,但它的行為就像2.x版本的long 2)新增了bytes類型,對(duì)應(yīng)于2.x版本的八位串,定義一個(gè)bytes字面量的方法如下: >>> b = bchina >>> type(b) <type bytes> str對(duì)象和bytes對(duì)象可以使用.encode() (str -> bytes) or .d

9、ecode() (bytes -> str)方法相互轉(zhuǎn)化。 >>> s = b.decode() >>> s china >>> b1 = s.encode() >>> b1 bchina 3)dict的.keys()、.items 和.values()方法返回迭代器,而之前的iterkeys()等函數(shù)都被廢棄。同時(shí)去掉的還有 dict.has_key(),用 in替代它吧 6.面向?qū)ο?1)引入抽象基類(abstraact base classes,abcs)。 2)容器類和迭代器類被abcs化,所以cellect

10、ions模塊里的類型比py2.5多了很多。 >>> import collections >>> print(n.join(dir(collections) callable container hashable itemsview iterable iterator keysview mapping mappingview mutablemapping mutablesequence mutableset namedtuple sequence set sized valuesview _all_ _builtins_ _doc_ _file_ _name

11、_ _abcoll _itemgetter _sys defaultdict deque 另外,數(shù)值類型也被abcs化。關(guān)于這兩點(diǎn),請(qǐng)參閱 pep 3119和pep 3141。 3)迭代器的next()方法改名為_(kāi)next_(),并增加內(nèi)置函數(shù)next(),用以調(diào)用迭代器的_next_()方法 4)增加了abstractmethod和 abstractproperty兩個(gè) decorator,編寫(xiě)抽象方法(屬性)更加方便。 7.異常 1)所以異常都從 baseexception繼承,并刪除了stardarderror 2)去除了異常類的序列行為和.message屬性 3)用 raise exc

12、eption(args)代替 raise exception, args語(yǔ)法 4)捕獲異常的語(yǔ)法改變,引入了as關(guān)鍵字來(lái)標(biāo)識(shí)異常實(shí)例,在py2.5中: >>> try: . raise notimplementederror(error) . except notimplementederror, error: . print error.message . error 在py3.0中: >>> try: raise notimplementederror(error) except notimplementederror as error: #注意這個(gè) a

13、s print(str(error) error 5)異常鏈,因?yàn)開(kāi)context_在3.0a1版本中沒(méi)有實(shí)現(xiàn) 8.模塊變動(dòng) 1)移除了cpickle模塊,可以使用pickle模塊代替。最終我們將會(huì)有一個(gè)透明高效的模塊。 2)移除了imageop模塊 3)移除了 audiodev, bastion, bsddb185, exceptions, linuxaudiodev, md5, mimewriter, mimify, popen2, rexec, sets, sha, stringold, strop, sunaudiodev, timing和xmllib模塊 4)移除了bsddb模塊(單

14、獨(dú)發(fā)布,可以從http:/www.jcea.es/programacion/pybsddb.htm獲取) 5)移除了new模塊 6)os.tmpnam()和os.tmpfile()函數(shù)被移動(dòng)到tmpfile模塊下 7)tokenize模塊現(xiàn)在使用bytes工作。主要的入口點(diǎn)不再是generate_tokens,而是 tokenize.tokenize() 9.其它 1)xrange() 改名為range(),要想使用range()獲得一個(gè)list,必須顯式調(diào)用: >>> list(range(10) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 2)bytes對(duì)象

15、不能hash,也不支持 b.lower()、b.strip()和b.split()方法,但對(duì)于后兩者可以使用 b.strip(b ntr f)和b.split(b )來(lái)達(dá)到相同目的 3)zip()、map()和filter()都返回迭代器。而apply()、 callable()、coerce()、 execfile()、reduce()和reload ()函數(shù)都被去除了 現(xiàn)在可以使用hasattr()來(lái)替換 callable(). hasattr()的語(yǔ)法如:hasattr(string, _name_) 4)string.letters和相關(guān)的.lowercase和.uppercase被去除,請(qǐng)改用string.ascii_letters 等 5)如果x < y的不能比較,拋出typeerror異常。2.x版本是返回偽隨機(jī)布爾值的 6)_getslice_系列成員被廢棄。ai:j根據(jù)上下文轉(zhuǎn)換為a._getitem_(slice

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論