79527263

Date: 2025-03-22 10:16:38
Score: 1
Natty:
Report link

I believe a lot has changed in pyo3 since the last answer. Also I want to give a complete example including the return value and the acquisition of the GIL.

#[pyfunction]
pub fn get_heterogeneous_python_dict()  -> PyResult<PyObject> {
    Python::with_gil(|py| {
        let d = PyDict::new(py);
        d.set_item("str", "asd").unwrap();
        d.set_item("num", 8).unwrap();
        Ok(d.into())
    })        
}

Proving on the Python side the heterogeneous types

def test_get_heterogeneous_python_dict(self):
    d = get_heterogeneous_python_dict()
    self.assertEqual(d["num"], 8)
    self.assertEqual(d["str"], "asd")
    print(d)
Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: skirc