Python的每個新版本都會增加一些新的功能,或者對原來的功能作一些改動。有些改動是不兼容舊版本的,也就是在當前版本運行正常的代碼,到下一個版本運行就可能不正常了。
從Python 2.7到Python 3.x就有不兼容的一些改動,比如2.x里的字符串用'xxx'表示str,Unicode字符串用u'xxx'表示unicode,而在3.x中,所有字符串都被視為unicode,因此,寫u'xxx'和'xxx'是完全一致的,而在2.x中以'xxx'表示的str就必須寫成b'xxx',以此表示“二進制字符串”。
要直接把代碼升級到3.x是比較冒進的,因為有大量的改動需要測試。相反,可以在2.7版本中先在一部分代碼中測試一些3.x的特性,如果沒有問題,再移植到3.x不遲。
Python提供了__future__模塊,把下一個新版本的特性導入到當前版本,于是我們就可以在當前版本中測試一些新版本的特性。舉例說明如下:
為了適應Python 3.x的新的字符串的表示方法,在2.7版本的代碼中,可以通過unicode_literals來使用Python 3.x的新的語法:
1
2
3
4
5
6
7
8
|
# still running on Python 2.7 from __future__ import unicode_literals print '\'xxx\' is unicode?' , isinstance ( 'xxx' , unicode ) print 'u\'xxx\' is unicode?' , isinstance (u 'xxx' , unicode ) print '\'xxx\' is str?' , isinstance ( 'xxx' , str ) print 'b\'xxx\' is str?' , isinstance (b 'xxx' , str ) |
注意到上面的代碼仍然在Python 2.7下運行,但結果顯示去掉前綴u的'a string'仍是一個unicode,而加上前綴b的b'a string'才變成了str:
1
2
3
4
5
|
$ python task.py 'xxx' is unicode ? True u 'xxx' is unicode ? True 'xxx' is str ? False b 'xxx' is str ? True |
類似的情況還有除法運算。在Python 2.x中,對于除法有兩種情況,如果是整數相除,結果仍是整數,余數會被扔掉,這種除法叫“地板除”:
1
2
|
>>> 10 / 3 3 |
要做精確除法,必須把其中一個數變成浮點數:
1
2
|
>>> 10.0 / 3 3.3333333333333335 |
而在Python 3.x中,所有的除法都是精確除法,地板除用//表示:
1
2
3
4
5
6
7
8
|
$ python3 Python 3.3 . 2 (default, Jan 22 2014 , 09 : 54 : 40 ) [GCC 4.2 . 1 Compatible Apple LLVM 5.0 (clang - 500.2 . 79 )] on darwin Type "help" , "copyright" , "credits" or "license" for more information. >>> 10 / 3 3.3333333333333335 >>> 10 / / 3 3 |
如果你想在Python 2.7的代碼中直接使用Python 3.x的除法,可以通過__future__模塊的division實現:
1
2
3
4
5
|
from __future__ import division print '10 / 3 =' , 10 / 3 print '10.0 / 3 =' , 10.0 / 3 print '10 // 3 =' , 10 / / 3 |
結果如下:
1
2
3
|
10 / 3 = 3.33333333333 10.0 / 3 = 3.33333333333 10 / / 3 = 3 |
小結
由于Python是由社區推動的開源并且免費的開發語言,不受商業公司控制,因此,Python的改進往往比較激進,不兼容的情況時有發生。Python為了確保你能順利過渡到新版本,特別提供了__future__模塊,讓你在舊的版本中試驗新版本的一些特性。