Skip to content

Scope of Python function called from C++ #3450

Answered by danimasa
danimasa asked this question in Q&A
Discussion options

You must be logged in to vote

I find a way to read the local variables from inside the function. What I did was update the globals before running the function:

auto local = py::dict();
auto globals = py::globals();
py::eval_file("myfile.py", globals, local);
globals.attr("update")(locals);
py::object func = local["my_func"];
func();

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by danimasa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant