EX.Evaluation time discrepancy

This commit is contained in:
暮晨 2018-11-13 22:23:52 +08:00 committed by 暮晨
parent 8ad4bef313
commit 6911e5b1ec

View File

@ -30,7 +30,7 @@ So, here we go...
- [> Return return everywhere!/到处返回!](#-return-return-everywhere到处返回)
- [> Deep down, we're all the same./本质上,我们都一样. *](#-deep-down-were-all-the-same本质上我们都一样-)
- [> For what?/为什么?](#-for-what为什么)
- [> Evaluation time discrepancy](#-evaluation-time-discrepancy)
- [> Evaluation time discrepancy/执行时机差异](#-evaluation-time-discrepancy执行时机差异)
- [> `is` is not what it is!](#-is-is-not-what-it-is)
- [> A tic-tac-toe where X wins in the first attempt!](#-a-tic-tac-toe-where-x-wins-in-the-first-attempt)
- [> The sticky output function](#-the-sticky-output-function)
@ -377,7 +377,7 @@ for i, some_dict[i] in enumerate(some_string):
---
### > Evaluation time discrepancy
### > Evaluation time discrepancy/执行时机差异
1\.
```py
@ -413,13 +413,13 @@ array_2[:] = [1,2,3,4,5]
[1,2,3,4,5]
```
#### 💡 Explanation
#### 💡 说明
- 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.
- 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`.
- 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.
- 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).
- 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]`).
- 在[生成器](https://wiki.python.org/moin/Generators)表达式中, `in` 子句在声明时执行, 而条件子句则是在运行时执行.
- 所以在运行前, `array` 已经被重新赋值为 `[2, 8, 22]`, 因此对于之前的 `1`, `8``15`, 只有 `count(8)` 的结果是大于 `0` 的, 所以生成器只会生成 `8`.
- 第二部分中 `g1``g2` 的输出差异则是由于变量 `array_1``array_2` 被重新赋值的方式导致的.
- 在第一种情况下, `array_1` 被绑定到新对象 `[1,2,3,4,5]`, 因为 `in` 子句是在声明时被执行的, 所以它仍然引用旧对象 `[1,2,3,4]`(并没有被销毁).
- 在第二种情况下, 对 `array_2` 的切片赋值将相同的旧对象 `[1,2,3,4]` 原地更新为 `[1,2,3,4,5]`. 因此 `g2``array_2` 仍然引用同一个对象(这个对象现在已经更新为 `[1,2,3,4,5]`).
---