为什么Python优化"if 0",而不是"if None"?

Meh*_*dad 19 python cpython python-2.7

如果你编译一个像这样的条件表达式,为什么呢?

def f():
    if None:
        print(222)
    if 0:
        print(333)
Run Code Online (Sandbox Code Playgroud)

使用数字的分支得到优化,但那些使用的分支None没有?例:

 3        0 LOAD_CONST               0 (None)
          3 POP_JUMP_IF_FALSE       14

 4        6 LOAD_CONST               1 (222)
          9 PRINT_ITEM          
         10 PRINT_NEWLINE       
         11 JUMP_FORWARD             0 (to 14)

 5  >>   14 LOAD_CONST               0 (None)
         17 RETURN_VALUE        
Run Code Online (Sandbox Code Playgroud)

在哪些情况下可以if 0if None表现不同?

MSe*_*ert 8

我的猜测:这是一个疏忽,因为None它只是python-2.x中一个特殊的名称(或全局).

如果你看一下python-2.x中的字节码优化器代码:

switch (opcode) {

   /* ... More cases ... */

        /* Replace LOAD_GLOBAL/LOAD_NAME None
           with LOAD_CONST None */
    case LOAD_NAME:
    case LOAD_GLOBAL:
        j = GETARG(codestr, i);
        name = PyString_AsString(PyTuple_GET_ITEM(names, j));
        if (name == NULL  ||  strcmp(name, "None") != 0)
            continue;
        for (j=0 ; j < PyList_GET_SIZE(consts) ; j++) {
            if (PyList_GET_ITEM(consts, j) == Py_None)
                break;
        }
        if (j == PyList_GET_SIZE(consts)) {
            if (PyList_Append(consts, Py_None) == -1)
                goto exitError;
        }
        assert(PyList_GET_ITEM(consts, j) == Py_None);
        codestr[i] = LOAD_CONST;
        SETARG(codestr, i, j);
        cumlc = lastlc + 1;
        break;      /* Here it breaks, so it can't fall through into the next case */

        /* Skip over LOAD_CONST trueconst
           POP_JUMP_IF_FALSE xx. This improves
           "while 1" performance. */
    case LOAD_CONST:
        cumlc = lastlc + 1;
        j = GETARG(codestr, i);
        if (codestr[i+3] != POP_JUMP_IF_FALSE  ||
            !ISBASICBLOCK(blocks,i,6)  ||
            !PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
            continue;
        memset(codestr+i, NOP, 6);
        cumlc = 0;
        break;

   /* ... More cases ... */

}
Run Code Online (Sandbox Code Playgroud)

您可能会注意到None已加载LOAD_GLOBALLOAD_NAME然后替换为LOAD_CONST.

但是:在更换它之后break,所以它不能进入如果常量不是LOAD_CONST用块替换块的情况.NOPTrue


在python-3.x中,优化器不需要特殊情况下的名称(或全局),None因为它总是被加载LOAD_CONST并且字节码优化器读取:

switch (opcode) {

   /* ... More cases ... */

        /* Skip over LOAD_CONST trueconst
           POP_JUMP_IF_FALSE xx.  This improves
           "while 1" performance.  */
    case LOAD_CONST:
        CONST_STACK_PUSH_OP(i);
        if (nextop != POP_JUMP_IF_FALSE  ||
            !ISBASICBLOCK(blocks, op_start, i + 1)  ||
            !PyObject_IsTrue(PyList_GET_ITEM(consts, get_arg(codestr, i))))
            break;
        fill_nops(codestr, op_start, nexti + 1);
        CONST_STACK_POP(1);
        break;

   /* ... More cases ... */

}
Run Code Online (Sandbox Code Playgroud)

有没有针对特殊情况LOAD_NAMELOAD_GLOBAL了,所以if None(也if False- False也有人提出在Python-3.X常数)将进入LOAD_CONST的情况下,然后以一取代NOP.