Skip to content

Commit 3722a01

Browse files
author
暮晨
committed
EX.Evaluation time discrepancy
1 parent 247c468 commit 3722a01

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
@@ -30,7 +30,7 @@ So, here we go...
3030
- [> Return return everywhere!/到处返回!](#-return-return-everywhere到处返回)
3131
- [> Deep down, we're all the same./本质上,我们都一样. *](#-deep-down-were-all-the-same本质上我们都一样-)
3232
- [> For what?/为什么?](#-for-what为什么)
33-
- [> Evaluation time discrepancy](#-evaluation-time-discrepancy)
33+
- [> Evaluation time discrepancy/评估时间差异](#-evaluation-time-discrepancy评估时间差异)
3434
- [> `is` is not what it is!](#-is-is-not-what-it-is)
3535
- [> A tic-tac-toe where X wins in the first attempt!](#-a-tic-tac-toe-where-x-wins-in-the-first-attempt)
3636
- [> The sticky output function](#-the-sticky-output-function)
@@ -375,7 +375,7 @@ for i, some_dict[i] in enumerate(some_string):
375375
376376
---
377377
378-
### > Evaluation time discrepancy
378+
### > Evaluation time discrepancy/评估时间差异
379379
380380
1\.
381381
```py
@@ -411,13 +411,13 @@ array_2[:] = [1,2,3,4,5]
411411
[1,2,3,4,5]
412412
```
413413
414-
#### 💡 Explanation
414+
#### 💡 说明
415415
416-
- In a [generator](https://wiki.python.org/moin/Generators) expression, the `in` clause is evaluated at declaration time, but the conditional clause is evaluated at runtime.
417-
- So before runtime, `array` is re-assigned to the list `[2, 8, 22]`, and since out of `1`, `8` and `15`, only the count of `8` is greater than `0`, the generator only yields `8`.
418-
- The differences in the output of `g1` and `g2` in the second part is due the way variables `array_1` and `array_2` are re-assigned values.
419-
- In the first case, `array_1` is binded to the new object `[1,2,3,4,5]` and since the `in` clause is evaluated at the declaration time it still refers to the old object `[1,2,3,4]` (which is not destroyed).
420-
- In the second case, the slice assignment to `array_2` updates the same old object `[1,2,3,4]` to `[1,2,3,4,5]`. Hence both the `g2` and `array_2` still have reference to the same object (which has now been updated to `[1,2,3,4,5]`).
416+
- 在[生成器](https://wiki.python.org/moin/Generators)表达式中, `in` 子句在声明时执行, 而条件子句则是在运行时执行.
417+
- 所以在运行前, `array` 已经被重新赋值为 `[2, 8, 22]`, 因此对于之前的 `1`, `8` `15`, 只有 `count(8)` 的结果是大于 `0` 的, 所以生成器只会生成 `8`.
418+
- 第二部分中 `g1` `g2` 的输出差异则是由于变量 `array_1` `array_2` 被重新赋值的方式导致的.
419+
- 在第一种情况下, `array_1` 被绑定到新对象 `[1,2,3,4,5]`, 因为 `in` 子句是在声明时被执行的, 所以它仍然引用旧对象 `[1,2,3,4]`(并没有被销毁).
420+
- 在第二种情况下, 对 `array_2` 的切片赋值将相同的旧对象 `[1,2,3,4]` 原地更新为 `[1,2,3,4,5]`. 因此 `g2` `array_2` 仍然引用同一个对象(这个对象现在已经更新为 `[1,2,3,4,5]`).
421421
422422
---
423423

0 commit comments

Comments
 (0)