-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add example for calling between rust and python #4276
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
Conversation
Is there a way that you can have a |
You might set git email wrong (not same as your github id email) |
to use |
I fixed the formatting.
Yeah, I'm intentionally setting the email to a random one because I don't like having the other one associated with my account. However git refuses to not use the global config which causes me to forget and have to amend the email with a force push. I hope it's not important that the email is set the same as my github email?
Alright, then I'll leave the import business be as it is. |
It will be ok if the email is real, but I think this is not ideal
You can set local name and email which doesn't affect global config using |
Alright, I will update the email address to a real one. I would like to put one more thing into the example which I realize is currently missing, which is to be able to use rust struct fields from inside of python. Something like: #[pyattr]
#[pyclass(module = "rust_py_module", name = "RustStruct")]
struct RustStruct {
pub number: f32
} python: from rust_py_module import RustStruct
def python_func():
rust_struct = rust_function()
print(rust_struct.number) Currently this is not working and I'm not sure how to accomplish it or if it is even possible, but I don't see any obvious reason for why it wouldn't be possible. |
That's not enough. Adding getset descriptor is necessary. impl RustStruct {
#[pygetset]
fn number(&self) -> f32 {
self.number
}
} we'd like to support member descriptor like below one day:
but not yet. cc @moreal |
Add an example which illustrates how to call between rust and python. Most importantly there no examples which illustrate how to call rust from python and it is not obvious how to do this.
In the example demonstrating how to call between rust and python add direct member access.
The direct member access is included and I've corrected the email to an existing one that I own. Let me know if there are any further changes you want (I had to leave that |
awesome! thank you! |
Thanks @Gelox! |
I am running into similar issues. I am looking at: https://github.com/RustPython/RustPython/blob/main/examples/call_between_rust_and_python.rs#L65-L83 Here is my question: on the Rust side, I have a RustStruct. Question: how do I bind this in the vm so python code can access this RustStruct ? Do I add it via VM, goobal scope? .... ? |
Add an example which illustrates how to call between rust and python. Most importantly there no examples which illustrate how to call rust from python and it is not obvious how to do this.
I never figured out if there was a way to nicely call a constructor of a rust struct since all methods in python require the self parameter I assumed that this was not possible and that construction should be done via a standalone function.
If it is encouraged or idiomatic to use a python constructor from rust for some reason then perhaps that should be added to the example.
Previous opened issue: #4124