From 2040b15dee25bf98c9c595648dafbfebadff02c2 Mon Sep 17 00:00:00 2001 From: Sohaib Farooqi Date: Sun, 28 Jan 2018 18:56:25 +0800 Subject: [PATCH 1/3] generator with slice assignment --- README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/README.md b/README.md index 68b6c093..16bd60c4 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ So, here we go... - [▶ Deep down, we're all the same. *](#-deep-down-were-all-the-same-) - [▶ For what?](#-for-what) - [▶ Evaluation time discrepancy](#-evaluation-time-discrepancy) + - [▶ Generator with Slice Assignment](#-generator-with-slice-assignment) - [▶ `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) @@ -383,6 +384,35 @@ array = [2, 8, 22] --- +--- +### ▶ Generator with Slice Assignment + +```py +iter1 = [1,2,3,4] +g1 = (x for x in x) +iter1 = [1,2,3,4,5] + +iter2 = [1,2,3,4] +g2 = (x for x in x) +iter2[:] = [1,2,3,4,5] + +**Output:** +```py +>>>print(list(g1)) +[1,2,3,4] + +>>> print(list(g2)) +[1,2,3,4,5] + +#### 💡 Explanation + +- In the first expression `g1` yields the elements of original list `iter1`(instead of updated one). Since the `in` +clause is evaluated at the time of generator declaration, hence generator expression `g1` still have reference of original list +`iter1` + +- 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. + + ### ▶ `is` is not what it is! The following is a very famous example present all over the internet. From 8e50e12f10653fdf576269c197311d3a1d41d7bf Mon Sep 17 00:00:00 2001 From: Sohaib Farooqi Date: Sun, 28 Jan 2018 18:58:08 +0800 Subject: [PATCH 2/3] revised formatting --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 16bd60c4..cfeefebd 100644 --- a/README.md +++ b/README.md @@ -384,7 +384,6 @@ array = [2, 8, 22] --- ---- ### ▶ Generator with Slice Assignment ```py @@ -395,6 +394,7 @@ iter1 = [1,2,3,4,5] iter2 = [1,2,3,4] g2 = (x for x in x) iter2[:] = [1,2,3,4,5] +``` **Output:** ```py @@ -403,6 +403,7 @@ iter2[:] = [1,2,3,4,5] >>> print(list(g2)) [1,2,3,4,5] +``` #### 💡 Explanation @@ -412,6 +413,7 @@ clause is evaluated at the time of generator declaration, hence generator expres - 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. +--- ### ▶ `is` is not what it is! From cd8cdc61f765a5bf7eb8a4247f7424c041d8ea2f Mon Sep 17 00:00:00 2001 From: Sohaib Farooqi Date: Mon, 29 Jan 2018 19:21:39 +0800 Subject: [PATCH 3/3] revised example code --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cfeefebd..9f825924 100644 --- a/README.md +++ b/README.md @@ -388,11 +388,11 @@ array = [2, 8, 22] ```py iter1 = [1,2,3,4] -g1 = (x for x in x) +g1 = (x for x in iter1) iter1 = [1,2,3,4,5] iter2 = [1,2,3,4] -g2 = (x for x in x) +g2 = (x for x in iter2) iter2[:] = [1,2,3,4,5] ```