英文:
How to add private methods to pyo3 pymethods?
问题
#[pyclass]
pub struct SomeItem;
#[pymethods]
impl SomeItem {
#[new]
pub fn new() -> Self { // visible for python constructor
SomeItem;
}
pub fn method(&self) -> u8 { // visible for python method
self.hidden_method()
}
#[pyo3(ignore)] // just for example
fn hidden_method(&self) -> u8 { // invisible for python method (that must be able to return non-python type)
0
}
}
英文:
I'd like to add hidden methods to pyo3 class methods implementation which will be invisible for Python.
Example:
#[pyclass]
pub struct SomeItem;
#[pymethods]
impl SomeItem {
#[new]
pub fn new() -> Self { // visible for python constructor
SomeItem;
}
pub fn method(&self) -> u8 { // visible for python method
self.hidden_method()
}
#[pyo3(ignore)] // just for example
fn hidden_method(&self) -> u8 { // invisible for python method (that must be able to return non-python type)
0
}
}
答案1
得分: 1
关于使用单独的 impl
块呢?
#[pymethods]
impl SomeItem {
#[new]
pub fn new() -> Self { // 对Python构造函数可见
SomeItem;
}
pub fn method(&self) -> u8 { // 对Python方法可见
self.hidden_method()
}
}
impl SomeItem {
fn hidden_method(&self) -> u8 {
0
}
}
英文:
What about using a separate impl
block?
#[pymethods]
impl SomeItem {
#[new]
pub fn new() -> Self { // visible for python constructor
SomeItem;
}
pub fn method(&self) -> u8 { // visible for python method
self.hidden_method()
}
}
impl SomeItem {
fn hidden_method(&self) -> u8 {
0
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论