iterator support (in ext. module)

H

Helmut Jarausch

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)

--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
V

vincent wehren

Helmut said:
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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
FloridaHan

Latest Threads

Top