Regarding extraction from &PyAny
(or PyObject
) to Rust object
#3940
-
Hello. I wanted to extract the Rust object from Python function call. Impl/// 'source is added because of below paragraph
impl<'source, I, O> SomeHiddenTrait<I, O> for SomeHiddenClass
where
I: ToPyObject,
O: PyClass + Clone,
{
fn update(&mut self, data: &I) -> anyhow::Result<O> {
Python::with_gil(|py| {
let args = PyTuple::new(py, &[data]);
let called = self.func.as_ref(py).call1(args)?;
let extracted = called.extract::<O>()?;
Ok(extracted)
})
}
} If I change What is the reason why |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The problem is that in the example you give above, To avoid this, either pass |
Beta Was this translation helpful? Give feedback.
The problem is that in the example you give above,
'source
is a lifetime limited to inside thewith_gil
closure.To avoid this, either pass
py: Python<'source>
as an argument, or useO: for<'a> FromPyObject<'a> + Clone
as the bound, which will limit the function to types which don't capture theFromPyObject
lifetime in their output.