pylist

Macro pylist 

Source
macro_rules! pylist {
    ($py:expr; $($value:expr),*) => { ... };
    ($($value:expr),*) => { ... };
}
Expand description

Create pyo3::types::PyList from a list of values.

§Examples

  • When you have GIL marker py, you can pass it and get a reference PyResult<&PyList>:
use pyo3::{Python, types::{PyList, PyListMethods, PyAnyMethods}};
use serde_pyobject::pylist;

Python::attach(|py| {
    let list = pylist![py; 1, "two"].unwrap();
    assert_eq!(list.len(), 2);
    assert_eq!(list.get_item(0).unwrap().extract::<i32>().unwrap(), 1);
    assert_eq!(list.get_item(1).unwrap().extract::<String>().unwrap(), "two");
})
  • When you don’t have GIL marker, you get a PyResult<Py<PyList>>:
use pyo3::{Python, Py, types::{PyList, PyListMethods, PyAnyMethods}};
use serde_pyobject::pylist;

let list: Py<PyList> = pylist![1, "two"].unwrap();

Python::attach(|py| {
   let list = list.into_bound(py);
   assert_eq!(list.len(), 2);
   assert_eq!(list.get_item(0).unwrap().extract::<i32>().unwrap(), 1);
   assert_eq!(list.get_item(1).unwrap().extract::<String>().unwrap(), "two");
});