是时候把你的Python2应用迁移到Python3了

原创
2019/02/24 14:27
阅读数 1K

到2020年一月1日,Python2.7将不再受到官方维护,小伙伴,程序猿,工程狮们,是时候将你们的Python2迁移到Python3了。因为距这一天只有10个月了!

许多的开源项目已经准备好离python2而去了:

上面的列表只是其中一些,包括了许多我常用的机器学习的库,Tensorflow,Pandas,Scikit-learn,Numpy等等,看看有没有你常用的呢?

Python2 VS Python3

那么我么就先来看看Python2/3的主要差异吧。

Python3引入了很多和Python2不兼容的关键字和功能,其中一些可以通过Python2内置的__future__模块来实现前向兼容,也就是说可以让你的Python2的代码在Python3的解释器中运行。如果你计划要支持Python3,那么你可以在你的Python2的代码中先使用该模块。

from __future__ import division

该模块支持的Python3的新特性如下的功能:

  • PEP 3105Make print a function
  • PEP 238Changing the Division Operator
  • PEP 3112Bytes literals in Python 3000 (unicode)
  • PEP 328Imports: Multi-Line and Absolute/Relative

print

print从2的statement变成了3的一个函数调用,在Python3调用print,必须使用函数的方式来调用,这个可能是最广为人知的Python3的变化了。

# 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

unicode

Python2的unicode,str,bytearray是三个不同的类型,而在3中,bytes和bytesarray都成为了类。Python3提供了对unicode的直接支持。

# 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 你好世界!

import

Python2和Python3对于import的主要区别在于:

  • Python2默认相对路径import,Python3默认绝对路径import
  • Python2需要在文件夹下创建 __init__.py文件才能把这个文件目录作为包来导入。在Python3.3以后,所有的目录都被看作是包,而无需__init__.py文件。
  • Python 2, 支持在函数哪调用 from <module> import * . Python 3, 像 from <module> import * 这样的语法只能在模块级别调用,不能在函数内调用

range and xrange

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了。

迭代器的next()方法

在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

For-loop variables

看栗子

# 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

Classes

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,那么你要做的是:

  1. 确定你的所有的依赖支持Python3
  2. 运行测试,确保你的所有的代码都能通过测试
  3. 进行代码迁移,这里你可能会用到一些工具
    1. six Python2 和 3 兼容库
    2. 利用 futurize 或者 modernize 
      Futurize就是在Python2中使用未来的Python3的功能,而Modernize正相反,使用Python2/3的兼容子集,利用six来提供兼容性。你可以选择任何一种方式。
    3. 使用pylint 来分析代码中和Python3不兼容的地方。
  4. 当你组件迁移到Python3,你可能会使用tox来保证兼容2,tox支持创建不同的python环境来进行测试。
  5. 开始使用类型检查

我们组的小伙伴已经兴高采烈,迫不及待的把代码都赶到Python3的一边了,你有没有计划呢?希望这篇文章能够帮助你下个决定吧!

参考

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部