Skip to content

Latest commit

 

History

History
28 lines (21 loc) · 430 Bytes

c01_28.md

File metadata and controls

28 lines (21 loc) · 430 Bytes

1.28 循环中的局部变量泄露

在Python 2中 x 的值在一个循环执行之后被改变了。

# Python2
>>> x = 1
>>> [x for x in range(5)]
[0, 1, 2, 3, 4]
>>> x
4

不过在Python3 中这个问题已经得到解决了。

# Python3
>>> x = 1
>>> [x for x in range(5)]
[0, 1, 2, 3, 4]
>>> x
1