到2020年一月1日,Python2.7将再也不受到官方维护,小伙伴,程序猿,工程狮们,是时候将大家的Python2迁移到Python3了。由于距这一天只有10个月了!html
许多的开源项目已经准备好离python2而去了:python
上面的列表只是其中一些,包括了许多我经常使用的机器学习的库,Tensorflow,Pandas,Scikit-learn,Numpy等等,看看有没有你经常使用的呢?git
那么我么就先来看看Python2/3的主要差别吧。github
Python3引入了不少和Python2不兼容的关键字和功能,其中一些能够经过Python2内置的__future__
模块来实现前向兼容,也就是说可让你的Python2的代码在Python3的解释器中运行。若是你计划要支持Python3,那么你能够在你的Python2的代码中先使用该模块。bash
from __future__ import division
该模块支持的Python3的新特性以下的功能:机器学习
print从2的statement变成了3的一个函数调用,在Python3调用print,必须使用函数的方式来调用,这个多是最广为人知的Python3的变化了。ide
# Python2 print is a statement print 'Hello, World!' print('Hello, World!') print "text", ; print 'print more text on the same line'
Hello, World! Hello, World! text print more text on the same line
# Python3 print is a function print('Hello, World!') print("some text,", end="") print(' print more text on the same line')
Hello, World! some text, print more text on the same line
除法运算函数
Python3除法运算的改动比较危险,由于改动的是计算的行为,语法上是彻底同样的,也就是说,一样的代码在2和3的环境下可能会返回不一样的结果。工具
#python2 Division print '3 / 2 =', 3 / 2 print '3 // 2 =', 3 // 2 print '3 / 2.0 =', 3 / 2.0 print '3 // 2.0 =', 3 // 2.0
3 / 2 = 1 3 // 2 = 1 3 / 2.0 = 1.5 3 // 2.0 = 1.0
# Python3 division print('3 / 2 =', 3 / 2) print('3 // 2 =', 3 // 2) print('3 / 2.0 =', 3 / 2.0) print('3 // 2.0 =', 3 // 2.0)
3 / 2 = 1.5 3 // 2 = 1 3 / 2.0 = 1.5 3 // 2.0 = 1.0
Python2的unicode,str,bytearray是三个不一样的类型,而在3中,bytes和bytesarray都成为了类。Python3提供了对unicode的直接支持。oop
# Python2 unicode/str/bytearray are 3 types print type(unicode('this is like a python3 str type')) print type(b'byte type does not exist') print 'they are really' + b' the same' print type(bytearray(b'bytearray oddly does exist though'))
<type 'unicode'> <type 'str'> they are really the same <type 'bytearray'>
# Python3 bytes and bytearray are two classed print(type(b' bytes for storing data')) print(type(bytearray(b'bytearrays'))) print('strings are now utf-8 \u4F60\u597D\u4E16\u754C!')
<class 'bytes'> <class 'bytearray'> strings are now utf-8 你好世界!
Python2和Python3对于import的主要区别在于:
from <module> import *
. Python 3, 像 from <module> import *
这样的语法只能在模块级别调用,不能在函数内调用Python2 range返回一个list,而Python3 range返回一个range类的对象。
# Python2 range print range(0,10,1) print type(range(0,10,1))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <type 'list'>
# Python3 range return a object print(range(0,10,1)) print(type(range(0,10,1))) print(list(range(0,10,1)))
range(0, 10) <class 'range'> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
xrange在Python2中常常被使用,咱们知道xrange是一个生成器。由于在3中range的行为和xrange同样,因此xrange在Python3中被delete了。
在Python2中,能够调用next函数或者迭代器对象的.next()方法,在Python3中,.next()方法被删除了。
# Python2 next my_generator = (letter for letter in 'abcdefg') print(next(my_generator)) print my_generator.next()
a b
# Python3 next my_generator = (letter for letter in 'abcdefg') print(next(my_generator))
a
看栗子
# Python2 Loop i = 1 print 'before: i =', i print 'comprehension: ', [i for i in range(5)] print 'after: i =', i
before: i = 1 comprehension: [0, 1, 2, 3, 4] after: i = 4
在Python2中,列推导表达式和它以外的代码拥有相同的命名空间,也就是说,for loop中的变量i和以前的全局变量i实际上是一个。这个多是一个危险的行为。
# Python3 loop i = 1 print('before: i =', i) print('comprehension:', [i for i in range(5)]) print('after: i =', i)
before: i = 1 comprehension: [0, 1, 2, 3, 4] after: i = 1
Python3的列推导表达式拥有独立的命名空间,不会污染到全局变量。
Python2,你能够对任何类型进行比较
# Python2 unordered type print "[1, 2] > 'foo' = ", [1, 2] > 'foo' print "(1, 2) > 'foo' = ", (1, 2) > 'foo' print "[1, 2] > (1, 2) = ", [1, 2] > (1, 2)
[1, 2] > 'foo' = False (1, 2) > 'foo' = True [1, 2] > (1, 2) = False
鬼才知道为何由1/2组成的列表比‘foo’要大,而一样的集合就小。Python3会返回Type Error
TypeError: unorderable types: list() > str()
Python2 采用的是四舍五入
# Python2 rounding print round(15.5) print round(16.5)
16.0 17.0
Python3采用国际最新的“银行家规则”,该规则并非简单“四舍六入五取偶”,而是只有在精确的 0.5 的状况下才会取偶,不然仍是要四舍五入的。
# Python3 banker's rounding print(round(15.5)) print(round(16.5))
16 16
IPython 2 支持两种类型的类 “old-style” 和 “new-style”或者“classic-style”. 而Python3只支持“new-style”
Type Annotation
Python2的类型标注是利用注释来实现的
# Python2 Type Hint def embezzle(self, account, funds=1000000, *fake_receipts): # type: (str, int, *str) -> None """Embezzle funds from account using fake receipts.""" <code goes here>
Python3.5开始支持原生的typing
# Python3 Typing def embezzle(self, account: str, funds :int =1000000, *fake_receipts : *str): -> None """Embezzle funds from account using fake receipts.""" <code goes here>
以上是一些主要和常见的差别,更多的细节你们能够在后面的参考中去找。
当你决定要迁移到Python3,那么你要作的是:
咱们组的小伙伴已经兴高采烈,火烧眉毛的把代码都赶到Python3的一边了,你有没有计划呢?但愿这篇文章可以帮助你下个决定吧!