Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
812 views
in Technique[技术] by (71.8m points)

pybind11 - Remove item from list in Pybind

I am moving CPython calls to pybind. I had the following code:

PyObject* my_list = ...;
PyList_SetSlice(my_list , i, i+1, NULL);

Which was used to remove an item. Now, switching to pybind11, I have

py::list my_list = ...;

And I would like to write something like

my_list.pop(i);

But I didn't find anything to do that. What should I do? Is the following correct:

PyList_SetSlice(my_list.ptr() , i, i+1, NULL);

Or am I leaking the object my_list[i]?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I am not sure why you don't advocate the syntax in @oooyaya's suggestion, but one way to remove an element at a specified index of the list would be to use pop() like

my_list.attr("pop")(index);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...