6

such as

In [9]: dis.disassemble(compile("s = '123' + '456'", "<execfile>", "exec"))
  1           0 LOAD_CONST               3 ('123456')
              3 STORE_NAME               0 (s)
              6 LOAD_CONST               2 (None)
              9 RETURN_VALUE 

I want to know, when does python combine the constant string as the CONST. If possible, please tell me which source code about this at cpython(whatever 2.x, 3.x).

codeforester
  • 39,467
  • 16
  • 112
  • 140
Dreampuf
  • 1,161
  • 1
  • 13
  • 28

2 Answers2

11

It happens whenever the combined string is 20 characters or fewer.

The optimization occurs in the peephole optimizer. See line 219 in the fold_binops_on_constants() function in Python/peephole.c: http://hg.python.org/cpython/file/cd87afe18ff8/Python/peephole.c#l149

codeforester
  • 39,467
  • 16
  • 112
  • 140
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
  • 6
    I wrote Python's peephole optimizer :-) – Raymond Hettinger Jan 24 '13 at 03:22
  • 1
    Investiganting for a [probably duplicate of this](https://stackoverflow.com/questions/49427151/how-does-python-determine-if-two-strings-are-identical/) I found out that folding has been recently moved to [`ast_opt.c`](https://github.com/python/cpython/blob/master/Python/ast_opt.c), and the maximum size for folded strings is now defined as 4096 (`MAX_STR_SIZE`). – jdehesa Mar 22 '18 at 12:07
4

@Raymond Hetting's answer is great, vote for that (I did). I'd make this a comment, but you can't format code in a comment.

If you go over the 20 character limit, the disassembly looks like:

>>> dis.disassemble(compile("s = '1234567890' + '09876543210'", "<execfile>", "exec"))
  1  0 LOAD_CONST  0 ('1234567890')
     3 LOAD_CONST  1 ('09876543210')
     6 BINARY_ADD
     7 STORE_NAME  0 (s)

But in the case where you have two string literals, remember you can leave out the + and use String literal concatenation to avoid the BINARY_ADD (even when the combined string length is greater than 20):

>>> dis.disassemble(compile("s = '1234567890' '09876543210'", "<execfile>", "exec"))
  1  0 LOAD_CONST  0 ('123456789009876543210')
     3 STORE_NAME  0 (s)
Community
  • 1
  • 1
jimhark
  • 4,938
  • 2
  • 27
  • 28