Polymorphism over python classes in rust #3453
-
Hello, I was wondering: Is it possible to: Define a parent in class in my rust api: #[pyclass(subclass)
pub struct ParentClass{
// stuff
}
#[pymethods]
impl ParentClass{
fn do_stuff(&self) {
// default implementation
}
fn get_two(&self) -> u8{
2
}
} Then, have python define children of this class: from rust_api import ParentClass
class Child(ParentClass):
def do_stuff(self):
# python version ...and have this new child class be usable at runtime by rust code (possibly loaded from something like I've seen something like this in the user guide with both the parent class/struct and child class/struct defined in rust. The implementation of a system like (how it relates to the GIL, lifetimes, safety, etc) this does not matter to me. I am just wondering if something like this was possible. Thanks :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You can do that, you just need to call methods via the Python API, not directly on the Rust level. |
Beta Was this translation helpful? Give feedback.
Yep exactly, see for example the older discussion #1875 (comment)