Type Alias pyo3::PyObject

source ·
pub type PyObject = Py<PyAny>;
Expand description

A commonly-used alias for Py<PyAny>.

This is an owned reference a Python object without any type information. This value can also be safely sent between threads.

See the documentation for Py.

Aliased Type§

struct PyObject(/* private fields */);

Implementations§

source§

impl PyObject

source

pub fn downcast<'py, T>( &'py self, py: Python<'py> ) -> Result<&'py T, PyDowncastError<'py>>
where T: PyTypeCheck<AsRefTarget = T>,

👎Deprecated since 0.21.0: PyObject::downcast will be replaced by PyObject::downcast_bound in a future PyO3 version

Deprecated form of PyObject::downcast_bound

source

pub fn downcast_bound<'py, T>( &self, py: Python<'py> ) -> Result<&Bound<'py, T>, DowncastError<'_, 'py>>
where T: PyTypeCheck,

Downcast this PyObject to a concrete Python type or pyclass.

Note that you can often avoid downcasting yourself by just specifying the desired type in function or method signatures. However, manual downcasting is sometimes necessary.

For extracting a Rust-only type, see Py::extract.

§Example: Downcasting to a specific Python object
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyList};

Python::with_gil(|py| {
    let any: PyObject = PyDict::new_bound(py).into();

    assert!(any.downcast_bound::<PyDict>(py).is_ok());
    assert!(any.downcast_bound::<PyList>(py).is_err());
});
§Example: Getting a reference to a pyclass

This is useful if you want to mutate a PyObject that might actually be a pyclass.

use pyo3::prelude::*;

#[pyclass]
struct Class {
    i: i32,
}

Python::with_gil(|py| {
    let class: PyObject = Py::new(py, Class { i: 0 }).unwrap().into_py(py);

    let class_bound = class.downcast_bound::<Class>(py)?;

    class_bound.borrow_mut().i += 1;

    // Alternatively you can get a `PyRefMut` directly
    let class_ref: PyRefMut<'_, Class> = class.extract(py)?;
    assert_eq!(class_ref.i, 1);
    Ok(())
})
source

pub unsafe fn downcast_unchecked<'py, T>(&'py self, py: Python<'py>) -> &T
where T: HasPyGilRef<AsRefTarget = T>,

👎Deprecated since 0.21.0: PyObject::downcast_unchecked will be replaced by PyObject::downcast_bound_unchecked in a future PyO3 version

Deprecated form of PyObject::downcast_bound_unchecked

§Safety

Callers must ensure that the type is valid or risk type confusion.

source

pub unsafe fn downcast_bound_unchecked<'py, T>( &self, py: Python<'py> ) -> &Bound<'py, T>

Casts the PyObject to a concrete Python object type without checking validity.

§Safety

Callers must ensure that the type is valid or risk type confusion.

Trait Implementations§

source§

impl<T> From<&T> for PyObject
where T: PyNativeType,

source§

fn from(obj: &T) -> Self

Converts to this type from the input type.
source§

impl<T> From<Bound<'_, T>> for PyObject
where T: AsRef<PyAny>,

source§

fn from(other: Bound<'_, T>) -> Self

Converts to this type from the input type.
source§

impl<T> From<Py<T>> for PyObject
where T: AsRef<PyAny>,

source§

fn from(other: Py<T>) -> Self

Converts to this type from the input type.