Specifics of List Membership

前端 未结 3 1626
旧时难觅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:34

    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.

提交回复
热议问题