Python slot_nb_bool(用于uuu bool的slot函数)如何调用uuu len_uu?
Python-3.5.1\Objects\typeobject.cPython slot_nb_bool(用于uuu bool的slot函数)如何调用uuu len_uu?,python,cpython,Python,Cpython,Python-3.5.1\Objects\typeobject.c UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred, "self != 0"), static int slot_nb_bool(PyObject *self) { PyObject *func, *args; int result = -1; int using_len = 0; _Py_IDENTIFIER(_
UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred,
"self != 0"),
static int
slot_nb_bool(PyObject *self)
{
PyObject *func, *args;
int result = -1;
int using_len = 0;
_Py_IDENTIFIER(__bool__);
func = lookup_maybe(self, &PyId___bool__);
if (func == NULL) {
if (PyErr_Occurred())
return -1;
func = lookup_maybe(self, &PyId___len__);
if (func == NULL)
return PyErr_Occurred() ? -1 : 1;
using_len = 1;
}
...
在我看来,只有在python源代码中定义了\uu bool\uu时才使用slot函数,如下所示:
def __bool__(self):
return True
如果func=lookup\u maybe(self,&PyId\uuuuuu\uuuu)
返回NULL,表示未定义\uuuu bool\uuuu
。然后插槽将不会被插槽\u nb\u bool填充。插槽\u nb\u bool将不会被调用
slot\u nb\u bool(用于
\u bool\u的slot函数)如何调用\u len\u
?如果定义了bool,查找可能(self,&PyId\u bool)
将返回某些内容,并将使用它。如果未定义bool,它将返回NULL,并且将尝试使用len。如果未定义bool,nb\u bool应为NULL,将不会调用slot\nb\u bool。