mirror of
https://github.com/satwikkansal/wtfpython
synced 2024-11-26 04:54:22 +01:00
Update evaluation time discrepency example: Add another snippet
Resolves https://github.com/satwikkansal/wtfpython/issues/41
This commit is contained in:
parent
3806284f78
commit
bc68ae0ff3
22
README.md
vendored
22
README.md
vendored
@ -408,7 +408,7 @@ for i, some_dict[i] in enumerate(some_string):
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### ▶ Evaluation time discrepancy
|
### ▶ Evaluation time discrepancy ^
|
||||||
|
|
||||||
1\.
|
1\.
|
||||||
```py
|
```py
|
||||||
@ -444,6 +444,23 @@ array_2[:] = [1,2,3,4,5]
|
|||||||
[1,2,3,4,5]
|
[1,2,3,4,5]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
3\.
|
||||||
|
|
||||||
|
```py
|
||||||
|
array_3 = [1, 2, 3]
|
||||||
|
array_4 = [10, 20, 30]
|
||||||
|
g = (i + j for i in array_3 for j in array_4)
|
||||||
|
|
||||||
|
array_3 = [4, 5, 6]
|
||||||
|
array_4 = [400, 500, 600]
|
||||||
|
```
|
||||||
|
|
||||||
|
**Output:**
|
||||||
|
```py
|
||||||
|
>>> print(list(g))
|
||||||
|
[401, 501, 601, 402, 502, 602, 403, 503, 603]
|
||||||
|
```
|
||||||
|
|
||||||
#### 💡 Explanation
|
#### 💡 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.
|
- 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.
|
||||||
@ -451,6 +468,9 @@ array_2[:] = [1,2,3,4,5]
|
|||||||
- 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.
|
- 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 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]`).
|
- 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]`).
|
||||||
|
- Okay, going by the logic discussed so far, shouldn't be the value of `list(g)` in the third snippet be `[11, 21, 31, 12, 22, 32, 13, 23, 33]`? (because `array_3` and `array_4` are going to behave just like `array_1`). The reason why (only) `array_4` values got updated is explained in [PEP-289](https://www.python.org/dev/peps/pep-0289/#the-details)
|
||||||
|
> Only the outermost for-expression is evaluated immediately, the other expressions are deferred until the generator is run.
|
||||||
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user