程序员文章、书籍推荐和程序员创业信息与资源分享平台

网站首页 > 技术文章 正文

python散装笔记——149: 从Python 2迁移到Python 3的不兼容性(三)

hfteth 2025-03-26 12:30:19 技术文章 8 ℃

Section 149.11: 迭代器上的.next()方法重命名

在Python 2中,可以通过在迭代器本身上使用next方法来遍历迭代器:

Python 2.x Version ≥ 2.3

g = (i for i in range(0, 3))
g.next() # Yields 0
g.next() # Yields 1
g.next() # Yields 2

在Python 3中,.next方法已被重命名为.__next__,以承认其“魔法”角色,因此调用.next将引发AttributeError。在Python 2和Python 3中正确访问此功能的方式是将迭代器作为参数调用next函数。

Python 3.x Version ≥ 3.0

g = (i for i in range(0, 3))
next(g) # Yields 0
next(g) # Yields 1
next(g) # Yields 2

此代码在2.6到当前版本之间具有可移植性。

Section 149.12: filter()、map()和zip()返回迭代器而不是序列

Python 2.x Version ≤ 2.7

在Python 2中,filtermapzip内置函数返回一个序列。mapzip始终返回一个list,而filter的返回类型取决于给定参数的类型:

>>> s = filter(lambda x: x.isalpha(), 'a1b2c3')
>>> s
'abc'
>>> s = map(lambda x: x * x, [0, 1, 2])
>>> s
[0, 1, 4]
>>> s = zip([0, 1, 2], [3, 4, 5])
>>> s
[(0, 3), (1, 4), (2, 5)]

Python 3.x Version ≥ 3.0

在Python 3中,filtermapzip返回迭代器而不是序列:

>>> it = filter(lambda x: x.isalpha(), 'a1b2c3')
>>> it

>>> ''.join(it)
'abc'
>>> it = map(lambda x: x * x, [0, 1, 2])
>>> it

>>> list(it)
[0, 1, 4]
>>> it = zip([0, 1, 2], [3, 4, 5])
>>> it

>>> list(it)
[(0, 3), (1, 4), (2, 5)]

由于Python 2中itertools.izip等同于Python 3中的zip,因此在Python 3中移除了izip

Section 149.13: 重命名的模块

标准库中的一些模块已被重命名:

旧名称

新名称

_winreg

winreg

ConfigParser

configparser

copy_reg

copyreg

Queue

queue

SocketServer

socketserver

_markupbase

markupbase

repr

reprlib

test.test_support

test.support

Tkinter

tkinter

tkFileDialog

tkinter.filedialog

urllib / urllib2

urllib, urllib.parse, urllib.error, urllib.response, urllib.request, urllib.robotparser

一些模块甚至从文件转换为库。以tkinterurllib为例。

兼容性

当在Python 2.x和3.x版本之间保持兼容性时,你可以使用外部包future来启用在Python 2.x版本中使用Python 3.x名称导入顶级标准库包。

Section 149.14: 移除<>和``运算符,分别等同于!=和repr()`

在Python 2中,<>!=的同义词;同样地,foorepr(foo)的同义词。

Python 2.x Version ≤ 2.7

>>> 1 <> 2
True
>>> 1 <> 1
False
>>> foo = 'hello world'
>>> repr(foo)
"'hello world'"
>>> `foo`
"'hello world'"

Python 3.x Version ≥ 3.0

>>> 1 <> 2
  File "", line 1
    1 <> 2
       ^
SyntaxError: invalid syntax
>>> `foo`
  File "", line 1
   `foo`
   ^
SyntaxError: invalid syntax

Section 149.15: long与int

在Python 2中,任何大于C ssize_t的整数都会被转换为long数据类型,这在字面量中用L后缀表示。例如,在32位构建的Python中:

Python 2.x Version ≤ 2.7

>>> 2**31
2147483648L
>>> type(2**31)

>>> 2**30
1073741824
>>> type(2**30)

>>> 2**31 - 1 # 2**31 is long and long - int is long
2147483647L

然而,在Python 3中,移除了long数据类型;无论整数有多大,它都将是int

Python 3.x Version ≥ 3.0

2**1024
# Output:
179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
print(-(2**1024))
# Output:
-179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
type(2**1024)
# Output: 

Tags:

最近发表
标签列表