Message383837
When unpacking a collection or string literal inside another literal, the compiler should optimise the unpacking away and store the resultant collection simply as another constant tuple, so that `[*'123', '4', '5']` is the exact same as `['1', '2', '3', '4', '5']`.
Compare:
```
>>> dis.dis("[*'123', '4', '5']")
1 0 BUILD_LIST 0
2 BUILD_LIST 0
4 LOAD_CONST 0 ('123')
6 LIST_EXTEND 1
8 LIST_EXTEND 1
10 LOAD_CONST 1 ('4')
12 LIST_APPEND 1
14 LOAD_CONST 2 ('5')
16 LIST_APPEND 1
```
vs.
```
>>> dis.dis("['1', '2', '3', '4', '5']")
1 0 BUILD_LIST 0
2 LOAD_CONST 0 (('1', '2', '3', '4', '5'))
4 LIST_EXTEND 1
```
and `timeit` shows the latter to be over 3 times as fast.
For example, when generating a list of characters, it is easier and more readable to do `alphabet = [*"abcde"]` instead of `alphabet = ["a", "b", "c", "d", "e"]`. The programmer can do what is most obvious without worrying about performance, because the compiler can do it itself. |
|
Date |
User |
Action |
Args |
2020-12-27 07:56:34 | pxeger | set | recipients:
+ pxeger |
2020-12-27 07:56:34 | pxeger | set | messageid: <1609055794.85.0.312864529219.issue42754@roundup.psfhosted.org> |
2020-12-27 07:56:34 | pxeger | link | issue42754 messages |
2020-12-27 07:56:34 | pxeger | create | |
|