tains__ method should define membership as applying to keys for mapping (and can use quick lookups)and as search for sequences consider the following classwhose file has been instrumented for dual / usage using the techniques described earlier it codes all three methods and tests membership and various iteration contexts applied to an instance its methods print trace messages when calledfile contains py from __future__ import print_function / compatibility class itersdef __init__(selfvalue)self data value def __getitem__(selfi)print('get[% ]:iend=''return self data[ifallback for iteration also for indexslice def __iter__(self)print('iter='end=''self ix return self preferred for iteration allows only one active iterator def __next__(self)print('next:'end=''if self ix =len(self data)raise stopiteration item self data[self ixself ix + return item def __contains__(selfx)print('contains'end=''return in self data next __next__ if __name__ ='__main__' iters([ ]print( in xfor in xprint(iend='print(print([ * for in ]printlist(map(binx)preferred for 'in / compatibility make instance membership for loops other iteration contexts iter(xmanual iteration (what other contexts dowhile truetryprint(next( )end='except stopiterationbreak membership__contains____iter__and __getitem__