# pyo3::pyclass_init::PyClassInitializer - Rust
Initializer for our `#[pyclass]` system.
Keywords: rust, rustlang, rust-lang, PyClassInitializer

# Struct pyo3::pyclass_init::PyClassInitializer [−] [src]

    pub struct PyClassInitializer<T: PyClass> { /* fields omitted */ }

Initializer for our `#[pyclass]` system.

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

# Example

    #[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" })
        }
    }
    let gil = Python::acquire_gil();
    let py = gil.python();
    let typeobj = py.get_type::<SubSubClass>();
    let inst = typeobj.call((), None).unwrap();
    py_run!(py, inst, r#"
            assert inst.basename == 'base'
            assert inst.subname == 'sub'
            assert inst.subsubname == 'subsub'"#);

## Implementations

### `impl<T: PyClass> PyClassInitializer<T>` [src]

#### `pub fn new(
init: T,
super_init: <T::BaseType as PyTypeInfo>::Initializer
) -> Self` [src]

Construct new initializer from value `T` and base class’ initializer.

We recommend to mainly use `add_subclass`, instead of directly call `new`.

#### `pub fn add_subclass<S>(self, subclass_value: S) -> PyClassInitializer<S> where
S: PyClass + PyTypeInfo<BaseType = T>,
S::BaseLayout: PySizedLayout<T>,
S::BaseType: PyTypeInfo<Initializer = Self>,` [src]

Constructs a new initializer from base class’ initializer.

# Example

    #[pyclass]
    struct BaseClass {
        value: u32,
    }

    impl BaseClass {
        fn new(value: i32) -> PyResult<Self> {
            Ok(Self {
                value: std::convert::TryFrom::try_from(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 {}))
        }
    }

## Trait Implementations

### `impl<S, B> From<(S, B)> for PyClassInitializer<S> where
S: PyClass + PyTypeInfo<BaseType = B>,
S::BaseLayout: PySizedLayout<B>,
B: PyClass + PyTypeInfo<Initializer = PyClassInitializer<B>>,
B::BaseType: PyTypeInfo<Initializer = PyNativeTypeInitializer<B::BaseType>>,` [src]

#### `fn from(sub_and_base: (S, B)) -> PyClassInitializer<S>` [src]

Performs the conversion.

### `impl<T> From<T> for PyClassInitializer<T> where
T: PyClass,
T::BaseType: PyTypeInfo<Initializer = PyNativeTypeInitializer<T::BaseType>>,` [src]

#### `fn from(value: T) -> PyClassInitializer<T>` [src]

Performs the conversion.

### `impl<T: PyClass> PyObjectInit<T> for PyClassInitializer<T>` [src]

#### `fn init_class<L: PyLayout<T>>(self, layout: &mut L)` [src]

#### `fn __private__(&self) -> PrivateMarker` [src]

This trait is private to implement; this method exists to make it impossible to implement outside the crate. Read more

## Auto Trait Implementations

### `impl<T> RefUnwindSafe for PyClassInitializer<T> where
T: RefUnwindSafe,
<<T as PyTypeInfo>::BaseType as PyTypeInfo>::Initializer: RefUnwindSafe,`

### `impl<T> Send for PyClassInitializer<T> where
T: Send,
<<T as PyTypeInfo>::BaseType as PyTypeInfo>::Initializer: Send,`

### `impl<T> Sync for PyClassInitializer<T> where
T: Sync,
<<T as PyTypeInfo>::BaseType as PyTypeInfo>::Initializer: Sync,`

### `impl<T> Unpin for PyClassInitializer<T> where
T: Unpin,
<<T as PyTypeInfo>::BaseType as PyTypeInfo>::Initializer: Unpin,`

### `impl<T> UnwindSafe for PyClassInitializer<T> where
T: UnwindSafe,
<<T as PyTypeInfo>::BaseType as PyTypeInfo>::Initializer: UnwindSafe,`

## Blanket Implementations

### `impl<T> Any for T where
T: 'static + ?Sized,` [src]

#### `pub fn type_id(&self) -> TypeId` [src]

Gets the `TypeId` of `self`. Read more

### `impl<T> Borrow<T> for T where
T: ?Sized,` [src]

#### `pub fn borrow(&self) -> &T` [src]

Immutably borrows from an owned value. Read more

### `impl<T> BorrowMut<T> for T where
T: ?Sized,` [src]

#### `pub fn borrow_mut(&mut self) -> &mut T` [src]

Mutably borrows from an owned value. Read more

### `impl<T> From<T> for T` [src]

#### `pub fn from(t: T) -> T` [src]

Performs the conversion.

### `impl<T, U> Into<U> for T where
U: From<T>,` [src]

#### `pub fn into(self) -> U` [src]

Performs the conversion.

### `impl<T, U> TryFrom<U> for T where
U: Into<T>,` [src]

#### `type Error = Infallible`

The type returned in the event of a conversion error.

#### `pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>` [src]

Performs the conversion.

### `impl<T, U> TryInto<U> for T where
U: TryFrom<T>,` [src]

#### `type Error = <U as TryFrom<T>>::Error`

The type returned in the event of a conversion error.

#### `pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>` [src]

Performs the conversion.