Skip to content

PyArray as PyClass attribute #357

Answered by adamreichold
MathieuMoalic asked this question in Q&A
Discussion options

You must be logged in to vote

You will generally not interact with PyArray<..> by value, but rather with &'py PyArray<..>, that is a shared reference to an ndarray object bound to a lifetime 'py for which the GIL is held. Or - and this probably applies here - with Py<PyArray<..>> that is a reference-counted ndarray with shared ownership managed by Python's heap.

This is not really specific to PyArray though but for example applies to most types from pyo3::types as well. You can read up on the details in PyO3's guide.

So long story short, you probably want

#[pyclass]
struct MyClass {
    #[pyo3(get, set)]
    array: Py<PyArray1<f32>>,
}

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@MathieuMoalic
Comment options

@MathieuMoalic
Comment options

@adamreichold
Comment options

@MathieuMoalic
Comment options

Answer selected by MathieuMoalic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants