emulating sequences

John Hunter jdhunter at nitace.bsd.uchicago.edu
Fri Aug 31 10:07:32 EDT 2001


I have a user defined type that I would like to behave like a
sequence, eg

results = MyType()
for result in results:
        blah

Talking about special methods, Beazley in 'Essential Python' says that
the following methods are used by objects that want to emulate
sequence and mapping objects:

__len__, __getitem__, __setitem__, __delitem__, __getslice__,
__setslice__, __delslice__

If I implement each of these for MyType, then can I get the desired
behavior?  If so, is there a subset that is sufficient just to get the
'in' behavior? 

Finally, if you have a sequence, what is the best way to get an index
of the first match of a value in a find command, similar to the C++

   i = find(s.begin(), s.end(), val)

Is there such a function, and if so, what is the illegal value
indicating failure to find?

Thanks,
John Hunter



More information about the Python-list mailing list