Skip to content

Commit fcfdbb8

Browse files
author
暮晨
committed
EX.Loop variables leaking out
1 parent cf9fde7 commit fcfdbb8

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ So, here we go...
5858
- [> Modifying a dictionary while iterating over it/迭代字典时的修改](#-modifying-a-dictionary-while-iterating-over-it迭代字典时的修改)
5959
- [> Stubborn `del` operator/坚强的 `del` *](#-stubborn-del-operator坚强的-del-)
6060
- [> Deleting a list item while iterating/迭代列表时删除元素](#-deleting-a-list-item-while-iterating迭代列表时删除元素)
61-
- [> Loop variables leaking out!](#-loop-variables-leaking-out)
61+
- [> Loop variables leaking out!/循环变量泄漏!](#-loop-variables-leaking-out循环变量泄漏)
6262
- [> Beware of default mutable arguments!](#-beware-of-default-mutable-arguments)
6363
- [> Catching the Exceptions](#-catching-the-exceptions)
6464
- [> Same operands, different story!](#-same-operands-different-story)
@@ -1480,7 +1480,7 @@ for idx, item in enumerate(list_4):
14801480
14811481
---
14821482
1483-
### > Loop variables leaking out!
1483+
### > Loop variables leaking out!/循环变量泄漏!
14841484
14851485
1\.
14861486
```py
@@ -1496,11 +1496,11 @@ print(x, ': x in global')
14961496
6 : x in global
14971497
```
14981498
1499-
But `x` was never defined outside the scope of for loop...
1499+
但是 `x` 从未在循环外被定义...
15001500
15011501
2\.
15021502
```py
1503-
# This time let's initialize x first
1503+
# 这次我们先初始化x
15041504
x = -1
15051505
for x in range(7):
15061506
if x == 6:
@@ -1533,13 +1533,13 @@ print(x, ': x in global')
15331533
1 : x in global
15341534
```
15351535
1536-
#### 💡 Explanation:
1536+
#### 💡 说明:
15371537
1538-
- In Python, for-loops use the scope they exist in and leave their defined loop-variable behind. This also applies if we explicitly defined the for-loop variable in the global namespace before. In this case, it will rebind the existing variable.
1538+
- Python, for 循环使用所在作用域并在结束后保留定义的循环变量. 如果我们曾在全局命名空间中定义过循环变量. 在这种情况下, 它会重新绑定现有变量.
15391539
1540-
- The differences in the output of Python 2.x and Python 3.x interpreters for list comprehension example can be explained by following change documented in [What’s New In Python 3.0](https://docs.python.org/3/whatsnew/3.0.html) documentation:
1540+
- Python 2.x Python 3.x 解释器在列表推导式示例中的输出差异, 在文档 [What’s New In Python 3.0](https://docs.python.org/3/whatsnew/3.0.html) 中可以找到相关的解释:
15411541
1542-
> "List comprehensions no longer support the syntactic form `[... for var in item1, item2, ...]`. Use `[... for var in (item1, item2, ...)]` instead. Also, note that list comprehensions have different semantics: they are closer to syntactic sugar for a generator expression inside a `list()` constructor, and in particular the loop control variables are no longer leaked into the surrounding scope."
1542+
> "列表推导不再支持句法形式 `[... for var in item1, item2, ...]`. 取而代之的是 `[... for var in (item1, item2, ...)]`. 另外, 注意列表推导具有不同的语义: 它们更接近于 `list()` 构造函数中生成器表达式的语法糖(译: 这一句我也不是很明白), 特别是循环控制变量不再泄漏到周围的作用域中."
15431543
15441544
---
15451545

0 commit comments

Comments
 (0)