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
impl PyObject
Sourcepub 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 versionAvailable on crate feature gil-refs
only.
pub fn downcast<'py, T>(
&'py self,
py: Python<'py>,
) -> Result<&'py T, PyDowncastError<'py>>where
T: PyTypeCheck<AsRefTarget = T>,
PyObject::downcast
will be replaced by PyObject::downcast_bound
in a future PyO3 versiongil-refs
only.Deprecated form of PyObject::downcast_bound
Sourcepub fn downcast_bound<'py, T>(
&self,
py: Python<'py>,
) -> Result<&Bound<'py, T>, DowncastError<'_, 'py>>where
T: PyTypeCheck,
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(())
})
Sourcepub unsafe fn downcast_unchecked<'py, T>(&'py self, py: Python<'py>) -> &Twhere
T: HasPyGilRef<AsRefTarget = T>,
👎Deprecated since 0.21.0: PyObject::downcast_unchecked
will be replaced by PyObject::downcast_bound_unchecked
in a future PyO3 versionAvailable on crate feature gil-refs
only.
pub unsafe fn downcast_unchecked<'py, T>(&'py self, py: Python<'py>) -> &Twhere
T: HasPyGilRef<AsRefTarget = T>,
PyObject::downcast_unchecked
will be replaced by PyObject::downcast_bound_unchecked
in a future PyO3 versiongil-refs
only.Deprecated form of PyObject::downcast_bound_unchecked
§Safety
Callers must ensure that the type is valid or risk type confusion.
Sourcepub unsafe fn downcast_bound_unchecked<'py, T>(
&self,
py: Python<'py>,
) -> &Bound<'py, T>
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.