-
This code works (i.e. static py::object base = py::none();
void take(int id) {
if (base.is_none())
base = py::list();
auto bl = py::list(base);
bl.append(id);
base = bl;
} However, it does not work when I remove the last line. Somebody please elucidate why, because I frankly don't understand it. Complete(d) example: py::object give() {
py::object res = base;
base = py::none();
return res;
}
NB_MODULE(ex3, m) {
m.def("take",&take);
m.def("give",&give);
} import example as e
e.take(1)
e.take(42)
assert e.give() == [1,42] |
Beta Was this translation helpful? Give feedback.
Answered by
wjakob
Feb 2, 2025
Replies: 1 comment 2 replies
-
… apparently I'm supposed to use |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not sure I appreciate the tone.
nb::list()
does the same thing aslist()
in Python, meaning it creates a copy. You can also borrow vianb::borrow<nb::list>()
if you know an object already has the right type.