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
+14-20
Original file line number
Diff line number
Diff line change
@@ -367,6 +367,7 @@ for i, some_dict[i] in enumerate(some_string):
367
367
368
368
### ▶ Evaluation time discrepancy
369
369
370
+
1\.
370
371
```py
371
372
array= [1, 8, 15]
372
373
g= (x for x in array if array.count(x) >0)
@@ -379,28 +380,21 @@ array = [2, 8, 22]
379
380
[8]
380
381
```
381
382
382
-
#### 💡 Explanation
383
-
384
-
- 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.
385
-
- 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`.
386
-
387
-
---
388
-
389
-
### ▶ Generator with Slice Assignment
383
+
2\.
390
384
391
385
```py
392
-
iter1= [1,2,3,4]
393
-
g1= (x for x initer1)
394
-
iter1= [1,2,3,4,5]
386
+
array_1= [1,2,3,4]
387
+
g1= (x for x inarray_1)
388
+
array_1= [1,2,3,4,5]
395
389
396
-
iter2= [1,2,3,4]
397
-
g2= (x for x initer2)
398
-
iter2[:] = [1,2,3,4,5]
390
+
array_2= [1,2,3,4]
391
+
g2= (x for x inarray_2)
392
+
array_2[:] = [1,2,3,4,5]
399
393
```
400
394
401
395
**Output:**
402
396
```py
403
-
>>>print(list(g1))
397
+
>>>print(list(g1))
404
398
[1,2,3,4]
405
399
406
400
>>>print(list(g2))
@@ -409,11 +403,11 @@ iter2[:] = [1,2,3,4,5]
409
403
410
404
#### 💡 Explanation
411
405
412
-
- In the first expression`g1` yields the elements of original list`iter1`(instead of updated one). Since the `in`
413
-
clause isevaluated at the time of generator declaration, hence generator expression `g1` still have reference of original list
414
-
`iter1`
415
-
416
-
- In the second example, list`iter1`is updated using slice assignment. Slice assignment, in contrast to normal assignment(which creates a new list), updates the same list. We can view this asthe slice of list being replace by the iterable on right hand side of equality. Hence, the generator `g2` yields updated `iter2` elements.
406
+
- 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.
407
+
- So before runtime, `array`isre-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`.
408
+
- 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.
409
+
- 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).
410
+
- In the second case, the slice assignmentto `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]`).
0 commit comments