C API q

E

Emanuel Landeholm

Hello!

I'm a Py neophyte trying to code an extension module in C. I would like
to know how, canonically, to remove a certain PyObject from a PyList,
from C. The solution I have come up with so far involves iterating
through the PyList keeping a counter, and then using
PyList_DelItem(counter) on the counter when the iter item == the
PyObject. But this is kind of messy. I'm sure there's a cleaner way to
do it?

TIA,
Emanuel
 
D

Diez B. Roggisch

Emanuel said:
Hello!

I'm a Py neophyte trying to code an extension module in C. I would like
to know how, canonically, to remove a certain PyObject from a PyList,
from C. The solution I have come up with so far involves iterating
through the PyList keeping a counter, and then using
PyList_DelItem(counter) on the counter when the iter item == the
PyObject. But this is kind of messy. I'm sure there's a cleaner way to
do it?

I doubt it - after all, the only access to a lists items is by
iterating/indexing over them, but not by id - so the remove() of a
python-list in the interpreter is only sugar over the procedure you
describe. But of cours you can write your own function and pass that a list
and an object :)
 
A

Alex Martelli

Emanuel Landeholm said:
Hello!

I'm a Py neophyte trying to code an extension module in C. I would like
to know how, canonically, to remove a certain PyObject from a PyList,
from C. The solution I have come up with so far involves iterating
through the PyList keeping a counter, and then using
PyList_DelItem(counter) on the counter when the iter item == the
PyObject. But this is kind of messy. I'm sure there's a cleaner way to
do it?

You can call the 'remove' method on the list object from C just as you
could from Python, essentially. Internally, that does essentially the
same thing you're doing here, but it does make your own code simpler.


Alex
 
E

Emanuel Landeholm

Alex said:
You can call the 'remove' method on the list object from C just as you
could from Python, essentially. Internally, that does essentially the
same thing you're doing here, but it does make your own code simpler.

Okay, how do I do that?
 

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

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,089
Members
47,689
Latest member
kilaocrhtbfnr

Latest Threads

Top