Python 源代码 使用 for(;;) 而不是 while 有什么意义?

Python source code What is the point of using for(;;) instead of while?

我喜欢 python 并希望做出贡献。我在 cpython 源代码中遇到了这一行。

for (;;) {
    item = iternext(it);
    if (item == NULL)
        break;
    cmp = PyObject_IsTrue(item);
    Py_DECREF(item);
    if (cmp < 0) {
        Py_DECREF(it);
        return NULL;
    }
    if (cmp > 0) {
        Py_DECREF(it);
        Py_RETURN_TRUE;
    }
}

使用 for(;;)

有什么意义
if (item == NULL)
        break;

而不是while(item!=NULL)是因为程序员希望iternext(it)至少执行一次吗?这对我来说似乎不是很直观,但由于它的 python 源代码,我相信一定有充分的理由?

这是一个中途退出的循环。要使其成为行为相同的 while 循环,您必须编写

item = iternext(it);
while (item != NULL) {
    cmp = PyObject_IsTrue(item);
    Py_DECREF(item);
    if (cmp < 0) {
        Py_DECREF(it);
        return NULL;
    }
    if (cmp > 0) {
        Py_DECREF(it);
        Py_RETURN_TRUE;
    }
    item = iternext(it);
}

但这意味着重复一行;它还将第二个 iternext 放在远离 while 语句的地方,从而使连接不那么明显。