Rust function that accepts a Python Iterable? #4256
Replies: 5 comments 2 replies
-
I guess I could've just done this: fn add_keywords_from_iter<'py>(&mut self, words: Bound<'py, PyIterator>) {
Python::with_gil(|_| {
let iter = words
.iter()
.unwrap()
.map(|py_obj| py_obj.unwrap().extract::<(String, String)>().unwrap());
self.inner.add_keywords_from_iter(iter);
})
} But I'm not sure if the use of the GIL token is correct |
Beta Was this translation helpful? Give feedback.
-
I would be interested in learning how to do this too, it would be really nice for creating Rust functions that feel more natural in Python. |
Beta Was this translation helpful? Give feedback.
-
This is how I did it: (my comment above is wrong, this accepts both |
Beta Was this translation helpful? Give feedback.
-
@jamestwebber check the message above |
Beta Was this translation helpful? Give feedback.
-
Thanks for that example, I was able to use that to get started and implement what I wanted. 🎉 One edge that I'm still a little unhappy with is the typing (in Rust). I find myself calling I have methods that look like:
It'd be slick to have something more like
Maybe I'm overthinking this, the current version is already good. But I was curious if there's a better way. |
Beta Was this translation helpful? Give feedback.
-
Hello, I want to make a Rust function that accepts a Python iterable (list, tuple, generator, etc.) of tuples:
Iterable[tuple[str, str]]
.I was using this code in v0.19 of PyO3, but now its not working anymore:
Any suggestions are welcome
Beta Was this translation helpful? Give feedback.
All reactions