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
Copy file name to clipboardExpand all lines: README.md
+22-2Lines changed: 22 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -408,7 +408,7 @@ for i, some_dict[i] in enumerate(some_string):
408
408
409
409
---
410
410
411
-
### ▶ Evaluation time discrepancy
411
+
### ▶ Evaluation time discrepancy ^
412
412
413
413
1\.
414
414
```py
@@ -444,13 +444,33 @@ array_2[:] = [1,2,3,4,5]
444
444
[1,2,3,4,5]
445
445
```
446
446
447
+
3\.
448
+
449
+
```py
450
+
array_3 = [1, 2, 3]
451
+
array_4 = [10, 20, 30]
452
+
g = (i + j for i in array_3 for j in array_4)
453
+
454
+
array_3 = [4, 5, 6]
455
+
array_4 = [400, 500, 600]
456
+
```
457
+
458
+
**Output:**
459
+
```py
460
+
>>>print(list(g))
461
+
[401, 501, 601, 402, 502, 602, 403, 503, 603]
462
+
```
463
+
447
464
#### 💡 Explanation
448
465
449
466
- 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.
450
467
- 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`.
451
468
- 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.
452
469
- 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 isnot destroyed).
453
470
- 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]`).
471
+
- 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)
472
+
> Only the outermost for-expression is evaluated immediately, the other expressions are deferred until the generator is run.
473
+
454
474
455
475
---
456
476
@@ -1221,7 +1241,7 @@ a, b = a[b] = {}, 5
1221
1241
and
1222
1242
1223
1243
> An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to right.
1224
-
1244
+
1225
1245
* The `+` in `(target_list "=")+` means there can be **one or more** target lists. In this case, target lists are `a, b` and `a[b]` (note the expression list is exactly one, which in our case is `{}, 5`).
1226
1246
1227
1247
* After the expression list is evaluated, it's value is unpacked to the target lists from **left to right**. So, in our case, first the `{}, 5` tuple is unpacked to `a, b` and we now have `a = {}` and `b = 5`.
0 commit comments