mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
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:
parent
0cc6b5e559
commit
8c579b1cc8
8 changed files with 145 additions and 18 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue