You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[> Beware of default mutable arguments!](#-beware-of-default-mutable-arguments)
63
63
-[> Catching the Exceptions](#-catching-the-exceptions)
64
64
-[> Same operands, different story!](#-same-operands-different-story)
@@ -1480,7 +1480,7 @@ for idx, item in enumerate(list_4):
1480
1480
1481
1481
---
1482
1482
1483
-
### > Loop variables leaking out!
1483
+
### > Loop variables leaking out!/循环变量泄漏!
1484
1484
1485
1485
1\.
1486
1486
```py
@@ -1496,11 +1496,11 @@ print(x, ': x in global')
1496
1496
6 : x in global
1497
1497
```
1498
1498
1499
-
But `x` was never defined outside the scope of for loop...
1499
+
但是 `x` 从未在循环外被定义...
1500
1500
1501
1501
2\.
1502
1502
```py
1503
-
# This time let's initialize x first
1503
+
# 这次我们先初始化x
1504
1504
x = -1
1505
1505
for x in range(7):
1506
1506
if x == 6:
@@ -1533,13 +1533,13 @@ print(x, ': x in global')
1533
1533
1 : x in global
1534
1534
```
1535
1535
1536
-
#### 💡 Explanation:
1536
+
#### 💡 说明:
1537
1537
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 循环使用所在作用域并在结束后保留定义的循环变量. 如果我们曾在全局命名空间中定义过循环变量. 在这种情况下, 它会重新绑定现有变量.
1539
1539
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) 中可以找到相关的解释:
1541
1541
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()` 构造函数中生成器表达式的语法糖(译: 这一句我也不是很明白), 特别是循环控制变量不再泄漏到周围的作用域中."
0 commit comments