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`.
This commit is contained in:
Serhiy Storchaka 2020-02-12 12:18:59 +02:00 committed by GitHub
parent 0cc6b5e559
commit 8c579b1cc8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 145 additions and 18 deletions

View file

@ -15,6 +15,22 @@ Test nesting with the inner expression dependent on the outer
>>> list((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.
>>> list((j*j for i in range(4) for j in [i+1]))
[1, 4, 9, 16]
>>> list((j*k for i in range(4) for j in [i+1] for k in [j+1]))
[2, 6, 12, 20]
>>> list((j*k for i in range(4) for j, k in [(i+1, i+2)]))
[2, 6, 12, 20]
Not assignment
>>> list((i*i for i in [*range(4)]))
[0, 1, 4, 9]
>>> list((i*i for i in (*range(4),)))
[0, 1, 4, 9]
Make sure the induction variable is not exposed
>>> i = 20