pub struct PyClassInitializer<T: PyClass> { /* private fields */ }
Expand description

Initializer for our #[pyclass] system.

You can use this type to initialize complicatedly nested #[pyclass].

Examples

#[pyclass(subclass)]
struct BaseClass {
    #[pyo3(get)]
    basename: &'static str,
}
#[pyclass(extends=BaseClass, subclass)]
struct SubClass {
    #[pyo3(get)]
    subname: &'static str,
}
#[pyclass(extends=SubClass)]
struct SubSubClass {
    #[pyo3(get)]
    subsubname: &'static str,
}

#[pymethods]
impl SubSubClass {
    #[new]
    fn new() -> PyClassInitializer<Self> {
        PyClassInitializer::from(BaseClass { basename: "base" })
            .add_subclass(SubClass { subname: "sub" })
            .add_subclass(SubSubClass {
                subsubname: "subsub",
            })
    }
}
Python::with_gil(|py| {
    let typeobj = py.get_type::<SubSubClass>();
    let sub_sub_class = typeobj.call((), None).unwrap();
    py_run!(
        py,
        sub_sub_class,
        r#"
 assert sub_sub_class.basename == 'base'
 assert sub_sub_class.subname == 'sub'
 assert sub_sub_class.subsubname == 'subsub'"#
    );
});

Implementations

Constructs a new initializer from value T and base class’ initializer.

It is recommended to use add_subclass instead of this method for most usage.

Constructs a new initializer from an initializer for the base class.

Examples
use pyo3::prelude::*;

#[pyclass(subclass)]
struct BaseClass {
    #[pyo3(get)]
    value: i32,
}

impl BaseClass {
    fn new(value: i32) -> PyResult<Self> {
        Ok(Self { value })
    }
}

#[pyclass(extends=BaseClass)]
struct SubClass {}

#[pymethods]
impl SubClass {
    #[new]
    fn new(value: i32) -> PyResult<PyClassInitializer<Self>> {
        let base_init = PyClassInitializer::from(BaseClass::new(value)?);
        Ok(base_init.add_subclass(SubClass {}))
    }
}

fn main() -> PyResult<()> {
    Python::with_gil(|py| {
        let m = PyModule::new(py, "example")?;
        m.add_class::<SubClass>()?;
        m.add_class::<BaseClass>()?;

        let instance = m.getattr("SubClass")?.call1((92,))?;

        // `SubClass` does not have a `value` attribute, but `BaseClass` does.
        let n = instance.getattr("value")?.extract::<i32>()?;
        assert_eq!(n, 92);

        Ok(())
    })
}

Trait Implementations

Converts to this type from the input type.
Converts to this type from the input type.
Safety Read more
This trait is private to implement; this method exists to make it impossible to implement outside the crate. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.