Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifics of List Membership

Tags:

python

list

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='C:\\')
    needle = (50, obj)
    haystack = [(50, fancy_obj(arg='C:\\')), (1, obj,), needle]

    print (1, fancy_obj(arg='C:\\'),) in haystack
    print needle in haystack

if __name__ == '__main__':
    main()

Which yields:

False
True

This tells me that Python is probably checking the object references, which makes sense. Is there something more definitive I can look at?

like image 586
Chris Avatar asked Jun 15 '10 22:06

Chris


People also ask

What do you mean by membership list?

Membership List means a list, in alphabetical order by name, setting forth the name, address and business or home telephone number of, and number of Shares held by, each Member, which list shall be printed on white paper in a readily readable type size (in no event smaller than 10-point type) and shall be updated at ...

What is the meaning of membership status?

Membership Status means the designation of a member as either Full Member, Alert Member, or Probationary Member.


3 Answers

From (An Unofficial) Python Reference Wiki:

For the list and tuple types, x in y is true if and only if there exists an index i such that x == y[i] is true.

So in your example, if the fancy_obj class stored the value of arg in an instance variable and were to implement an __eq__ method that returned True if the two fancy_objs being compared had the same value for arg then (1, fancy_obj(arg='C:\\'),) in haystack would be True.

The relevant page of the Standard Library reference is: Built-in Types, specifically 5.6 Sequence Types

like image 127
mikej Avatar answered Oct 04 '22 15:10

mikej


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.

like image 22
Justin Peel Avatar answered Oct 04 '22 15:10

Justin Peel


Python is using the (equivalent of) the == operator. If the fancy_obj class does not define __eq__ (or the crufty old __cmp__, still supported for backwards compatibility) then equality, ==, "falls back" to identity, is, and that appears to be what's happening here.

The relevant docs are here, and I quote:

x in s True if an item of s is equal to x, else False

and "equal to" means == is true.

like image 32
Alex Martelli Avatar answered Oct 04 '22 14:10

Alex Martelli