[Tutor] mistaken about splitting expressions over lines

eryksun eryksun at gmail.com
Tue Jun 25 17:43:57 CEST 2013


On Tue, Jun 25, 2013 at 11:35 AM, Peter Otten <__peter__ at web.de> wrote:
> eryksun wrote:
>
>> Constant folding for binary operations has a length limit of 20 for
>> sequences:
>>
>>     >>> dis.dis(lambda: '0123456789' + '0123456789' + '0')
>>       1           0 LOAD_CONST               3 ('0123456789
>>                                                  0123456789')
>>                   3 LOAD_CONST               2 ('0')
>>                   6 BINARY_ADD
>>                   7 RETURN_VALUE
>
> Interesting. Do you know why the limit is so low (especially for strings)?

It isn't special-cased for strings. It just checks for a sequence
length in general. The idea is to limit the size of .pyc files. Quote:

    If the new constant is a sequence, only folds when the size
    is below a threshold value.  That keeps pyc files from
    becoming large in the presence of code like:  (None,)*1000.

The threshold of 20 isn't a tunable parameter. It's hard-coded in the source:

    size = PyObject_Size(newconst);
    if (size == -1)
        PyErr_Clear();
    else if (size > 20) {
        Py_DECREF(newconst);
        return 0;
    }


More information about the Tutor mailing list