-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Pass a reference to C++ container to the Python callback #1200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Possibly related to #1231: This looks like it might also be a symptom of argument reference policies - it is most like likely passing a copy of your vector to your function. Can you try making your signature into |
…bind when the object is not already registered.
The main issue here in how return-value-policies propagate (or rather, don't propagate) through py::function f = reinterpret_borrow<py::function>(src);
py::object retval = f(py::cast(std::forward<Args>(args), py::return_value_policy::reference)...);
return retval.template cast<Return>(); but of course, that isn't right in many other cases: what we're missing is a way to specify the |
Jason @jagerman, totally agree with an approach of having |
My gut feeling is that explicitly specifying return value policies make sense for return values, but explicitly specifying input argument policies seems like it'd introduce an extra layer of complication (versus simply treating mutable lvalue references as reference-only, no copy). Can I ask what workflows there are where a (temporary) copy of an object for a mutable lvalue reference is actually desirable? That being said, it may line up more nicely with ownership transfer, like @torokati44's PR #1226. (I do have a little bit of bias here, as it'd be nice to let referencing / ownership be implicit for arguments, as they are done in C++, through the use of mutable lvalue references, |
Hi!
I faced this issue while implementing a Python's
os.walk
-like function for my tree structure. I hoped to mimic the ability of callback to edit passed list of directories in-place and guide further search.Suppose we have the following in C++:
And now the Python code:
Is there a way to specify that callback should take an opaque container by reference (in order to modify it in-place on Python side)?
The text was updated successfully, but these errors were encountered: