Skip to content

Commit 9608b2f

Browse files
committed
Merge examples
1 parent 70fe617 commit 9608b2f

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

README.md

+14-20
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ for i, some_dict[i] in enumerate(some_string):
367367
368368
### ▶ Evaluation time discrepancy
369369
370+
1\.
370371
```py
371372
array = [1, 8, 15]
372373
g = (x for x in array if array.count(x) > 0)
@@ -379,28 +380,21 @@ array = [2, 8, 22]
379380
[8]
380381
```
381382
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\.
390384
391385
```py
392-
iter1 = [1,2,3,4]
393-
g1 = (x for x in iter1)
394-
iter1 = [1,2,3,4,5]
386+
array_1 = [1,2,3,4]
387+
g1 = (x for x in array_1)
388+
array_1 = [1,2,3,4,5]
395389
396-
iter2 = [1,2,3,4]
397-
g2 = (x for x in iter2)
398-
iter2[:] = [1,2,3,4,5]
390+
array_2 = [1,2,3,4]
391+
g2 = (x for x in array_2)
392+
array_2[:] = [1,2,3,4,5]
399393
```
400394
401395
**Output:**
402396
```py
403-
>>>print(list(g1))
397+
>>> print(list(g1))
404398
[1,2,3,4]
405399
406400
>>> print(list(g2))
@@ -409,11 +403,11 @@ iter2[:] = [1,2,3,4,5]
409403
410404
#### 💡 Explanation
411405
412-
- In the first expression `g1` yields the elements of original list `iter1`(instead of updated one). Since the `in`
413-
clause is evaluated 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 as the 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` 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`.
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 is not destroyed).
410+
- 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]`).
417411
418412
---
419413

0 commit comments

Comments
 (0)