Called to implement membership test operators. Should return true if item is in self, false otherwise. For mapping objects, this should consider the keys of the mapping rather than the values or the key-item pairs.
>>> class Bar:
... def __init__(self, iterable):
... self.list = list(iterable)
... def __contains__(self, item):
... return item in self.list
>>>
>>> b = Bar([1,2,3])
>>> b.list
[1, 2, 3]
>>> 4 in b
False
>>> 2 in b
True
你在找^{} ?在
简单示例:
注意:通常当您有这种疑问时,可以在The Python Language Reference的Data Model部分找到参考。在
由于数据结构是一个链表,所以有必要对其进行迭代以检查成员身份。实现}同时工作。如果有更有效的方法来检查成员资格,请在
__iter__()
方法将使if in
和{__contains__()
中实现。在相关问题 更多 >
编程相关推荐