Skip to content

Overriding methods on PyO3 subclasses #4164

Discussion options

You must be logged in to vote

The problem is in speak:

    fn speak(&self) {
        println!("{}", self.greeting());
    }

self.greeting() still follows Rust method resolution, and Rust isn't aware of the class hierarchy defined in pyo3 annotations. This problem would also appear if the greeting method was implemented in a Python subclass of Animal.

The way out of this is to let the Python runtime resolve the method call target of the internal method:

    fn speak(slf: Py<Self>, py: Python) -> PyResult<()> {
        println!("{}", slf.call_method0(py, "greeting")?);
        Ok(())
    }

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@toby-coleman
Comment options

Answer selected by toby-coleman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants