Specifics of List Membership

前端 未结 3 1583
旧时难觅i
旧时难觅i 2021-01-18 08:46

How does Python (2.6.4, specifically) determine list membership in general? I\'ve run some tests to see what it does:

def main():
    obj = fancy_obj(arg=\'         


        
3条回答
  •  迷失自我
    2021-01-18 09:39

    Here is code from the Python SVN:

    static int
    list_contains(PyListObject *a, PyObject *el)
    {
        Py_ssize_t i;
        int cmp;
    
        for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
            cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
                                               Py_EQ);
        return cmp;
    }
    

    so basically it uses the == with the object and each object in the list.

提交回复
热议问题