iterator support (in ext. module)

vincent wehren vincent at visualtrans.de
Mon Jun 28 22:47:30 CEST 2004


Helmut Jarausch wrote:
> Hi,
> 
> sorry for the questions but I'm not sure I understand the
> C_API docu.
> 
> In an own extension module I have a new type called Hitlist,
> which has the methods  'first', 'last', 'next' and 'previous'.
> Now I'd like to support iterating on this type, e.g. like
> 
> HL= Hitlist(...)
> for Rcd in HL:
> 
> and also
> 
> for Rcd in HL.reverse() :
> 
> What do I need to implement to get that functionality?
> Unfortunately the chapter 10.8 'Supporting the Iterator Protocol' of the
> (devel) docu seems to be empty.
> 
> What should PyTypeObject.tp_iter be set to? Can I just return 'self'
> and is PyTypeObject.tp_iternext an alias for my 'next' method?
> 
> Does a call to 'PyTypeObject.tp_iter' reset the iterator s.t. 
> 'tp_iternext' returns the first object when called afterwards?
> 
> Many thanks for your help,
> Helmut.
> 
> (P.S. I'm using Version 2.4 -cvs)
> 

Maybe digging into the iterator PEP at 
http://www.python.org/peps/pep-0234.html
provides more info? In addition, you might want to look at the source of 
an object that uses the iterator protocol. PyDictIter_Type in 
dictobject.c for example.

HTH,
--
Vincent Wehren



More information about the Python-list mailing list