How do I get the class name from a PyRef<Self>
#2790
Unanswered
samuelcolvin
asked this question in
Questions
Replies: 1 comment
-
You can take use pyo3::prelude::*;
#[pyclass(subclass)]
pub struct Base;
#[pymethods]
impl Base {
#[new]
pub fn new() -> Self { Self }
fn which(slf: &PyCell<Self>) -> PyResult<&str> {
slf.get_type().name()
}
}
#[pymodule]
fn o3(py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<Base>()
} and this Python code: from o3 import Base
class Sub(Base):
pass
print(Base().which())
print(Sub().which()) it will print
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
If I have a type that implements,
#[pyclass(subclass...
, and I call a method on that "class", is it possible to get the name of the class on which that method was called in the method?The rust equivalent of
Beta Was this translation helpful? Give feedback.
All reactions