diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2020-02-12 10:18:59 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-12 10:18:59 (GMT) |
commit | 8c579b1cc86053473eb052b76327279476740c9b (patch) | |
tree | bc99d5f52e1330500216512d1064c2f341b64d89 /Lib/test/test_dictcomps.py | |
parent | 0cc6b5e559b8303b18fdd56c2befd900fe7b5e35 (diff) | |
download | cpython-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_dictcomps.py')
-rw-r--r-- | Lib/test/test_dictcomps.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/Lib/test/test_dictcomps.py b/Lib/test/test_dictcomps.py index 927e310..16aa651 100644 --- a/Lib/test/test_dictcomps.py +++ b/Lib/test/test_dictcomps.py @@ -111,5 +111,22 @@ class DictComprehensionTest(unittest.TestCase): self.assertEqual(actual, expected) self.assertEqual(actual_calls, expected_calls) + def test_assignment_idiom_in_comprehensions(self): + expected = {1: 1, 2: 4, 3: 9, 4: 16} + actual = {j: j*j for i in range(4) for j in [i+1]} + self.assertEqual(actual, expected) + expected = {3: 2, 5: 6, 7: 12, 9: 20} + actual = {j+k: j*k for i in range(4) for j in [i+1] for k in [j+1]} + self.assertEqual(actual, expected) + expected = {3: 2, 5: 6, 7: 12, 9: 20} + actual = {j+k: j*k for i in range(4) for j, k in [(i+1, i+2)]} + self.assertEqual(actual, expected) + + def test_star_expression(self): + expected = {0: 0, 1: 1, 2: 4, 3: 9} + self.assertEqual({i: i*i for i in [*range(4)]}, expected) + self.assertEqual({i: i*i for i in (*range(4),)}, expected) + + if __name__ == "__main__": unittest.main() |