-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Make it possible to provide scope to/store scope of Python function #1742
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
I tried many things and nothing worked. I must be missing some fundamental understanding of what locals and globals are. One thing worked, though: making global.attr("update")(local); I'd still be glad to hear why this is happening, though. |
I think the core of this is due to the, uh, interesting nuance of how Python's scoping for Modifying your above code corroborates this: All in all, it seems that if you want to bind variables to your lambda, consider passing them with Other alternatives are to implement your lambda using
|
Thanks for the explanation; in particular, the SO post was helpful. For my use case, I think that passing the local variables as globals is the best solution, but knowing alternatives is great as well! |
Had forgotten this stuff, so I wrote a quick unittest example in #2743. |
Seeing this now, after seeing #2743 (thanks, @EricCousineau-TRI!)
It should be noted there that |
Issue description
A lambda function constructed with a certain scope (
local
andglobal
) from withinpy::eval
cannot be called, as the scope is unavailable when the function is called.Reproducible example code
Output:
Initial discussion
The problem is that the local variable
a
is not available whenf
is called; in fact,f
does not seem to have a scope.Note that the following works in Python:
I have no idea how, but it would be great if there was a way to provide a scope to
py::function
s to make above example work.The text was updated successfully, but these errors were encountered: