summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_listcomps.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2020-02-12 10:18:59 (GMT)
committerGitHub <noreply@github.com>2020-02-12 10:18:59 (GMT)
commit8c579b1cc86053473eb052b76327279476740c9b (patch)
treebc99d5f52e1330500216512d1064c2f341b64d89 /Lib/test/test_listcomps.py
parent0cc6b5e559b8303b18fdd56c2befd900fe7b5e35 (diff)
downloadcpython-8c579b1cc86053473eb052b76327279476740c9b.zip
cpython-8c579b1cc86053473eb052b76327279476740c9b.tar.gz
cpython-8c579b1cc86053473eb052b76327279476740c9b.tar.bz2
bpo-32856: Optimize the assignment idiom in comprehensions. (GH-16814)
Now `for y in [expr]` in comprehensions is as fast as a simple assignment `y = expr`.
Diffstat (limited to 'Lib/test/test_listcomps.py')
-rw-r--r--Lib/test/test_listcomps.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/Lib/test/test_listcomps.py b/Lib/test/test_listcomps.py
index ddb169f..62b3319 100644
--- a/Lib/test/test_listcomps.py
+++ b/Lib/test/test_listcomps.py
@@ -16,6 +16,22 @@ Test nesting with the inner expression dependent on the outer
>>> [(i,j) for i in range(4) for j in range(i)]
[(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)]
+Test the idiom for temporary variable assignment in comprehensions.
+
+ >>> [j*j for i in range(4) for j in [i+1]]
+ [1, 4, 9, 16]
+ >>> [j*k for i in range(4) for j in [i+1] for k in [j+1]]
+ [2, 6, 12, 20]
+ >>> [j*k for i in range(4) for j, k in [(i+1, i+2)]]
+ [2, 6, 12, 20]
+
+Not assignment
+
+ >>> [i*i for i in [*range(4)]]
+ [0, 1, 4, 9]
+ >>> [i*i for i in (*range(4),)]
+ [0, 1, 4, 9]
+
Make sure the induction variable is not exposed
>>> i = 20