pyo3::types

Trait PyListMethods

Source
pub trait PyListMethods<'py>: Sealed {
Show 19 methods // Required methods fn len(&self) -> usize; fn is_empty(&self) -> bool; fn as_sequence(&self) -> &Bound<'py, PySequence>; fn into_sequence(self) -> Bound<'py, PySequence>; fn get_item(&self, index: usize) -> PyResult<Bound<'py, PyAny>>; unsafe fn get_item_unchecked(&self, index: usize) -> Bound<'py, PyAny>; fn get_slice(&self, low: usize, high: usize) -> Bound<'py, PyList>; fn set_item<I>(&self, index: usize, item: I) -> PyResult<()> where I: IntoPyObject<'py>; fn del_item(&self, index: usize) -> PyResult<()>; fn set_slice( &self, low: usize, high: usize, seq: &Bound<'_, PyAny>, ) -> PyResult<()>; fn del_slice(&self, low: usize, high: usize) -> PyResult<()>; fn append<I>(&self, item: I) -> PyResult<()> where I: IntoPyObject<'py>; fn insert<I>(&self, index: usize, item: I) -> PyResult<()> where I: IntoPyObject<'py>; fn contains<V>(&self, value: V) -> PyResult<bool> where V: IntoPyObject<'py>; fn index<V>(&self, value: V) -> PyResult<usize> where V: IntoPyObject<'py>; fn iter(&self) -> BoundListIterator<'py> ; fn sort(&self) -> PyResult<()>; fn reverse(&self) -> PyResult<()>; fn to_tuple(&self) -> Bound<'py, PyTuple>;
}
Expand description

Implementation of functionality for PyList.

These methods are defined for the Bound<'py, PyList> smart pointer, so to use method call syntax these methods are separated into a trait, because stable Rust does not yet support arbitrary_self_types.

Required Methods§

Source

fn len(&self) -> usize

Returns the length of the list.

Source

fn is_empty(&self) -> bool

Checks if the list is empty.

Source

fn as_sequence(&self) -> &Bound<'py, PySequence>

Returns self cast as a PySequence.

Source

fn into_sequence(self) -> Bound<'py, PySequence>

Returns self cast as a PySequence.

Source

fn get_item(&self, index: usize) -> PyResult<Bound<'py, PyAny>>

Gets the list item at the specified index.

§Example
use pyo3::{prelude::*, types::PyList};
Python::with_gil(|py| {
    let list = PyList::new(py, [2, 3, 5, 7]).unwrap();
    let obj = list.get_item(0);
    assert_eq!(obj.unwrap().extract::<i32>().unwrap(), 2);
});
Source

unsafe fn get_item_unchecked(&self, index: usize) -> Bound<'py, PyAny>

Gets the list item at the specified index. Undefined behavior on bad index. Use with caution.

§Safety

Caller must verify that the index is within the bounds of the list.

Source

fn get_slice(&self, low: usize, high: usize) -> Bound<'py, PyList>

Takes the slice self[low:high] and returns it as a new list.

Indices must be nonnegative, and out-of-range indices are clipped to self.len().

Source

fn set_item<I>(&self, index: usize, item: I) -> PyResult<()>
where I: IntoPyObject<'py>,

Sets the item at the specified index.

Raises IndexError if the index is out of range.

Source

fn del_item(&self, index: usize) -> PyResult<()>

Deletes the indexth element of self.

This is equivalent to the Python statement del self[i].

Source

fn set_slice( &self, low: usize, high: usize, seq: &Bound<'_, PyAny>, ) -> PyResult<()>

Assigns the sequence seq to the slice of self from low to high.

This is equivalent to the Python statement self[low:high] = v.

Source

fn del_slice(&self, low: usize, high: usize) -> PyResult<()>

Deletes the slice from low to high from self.

This is equivalent to the Python statement del self[low:high].

Source

fn append<I>(&self, item: I) -> PyResult<()>
where I: IntoPyObject<'py>,

Appends an item to the list.

Source

fn insert<I>(&self, index: usize, item: I) -> PyResult<()>
where I: IntoPyObject<'py>,

Inserts an item at the specified index.

If index >= self.len(), inserts at the end.

Source

fn contains<V>(&self, value: V) -> PyResult<bool>
where V: IntoPyObject<'py>,

Determines if self contains value.

This is equivalent to the Python expression value in self.

Source

fn index<V>(&self, value: V) -> PyResult<usize>
where V: IntoPyObject<'py>,

Returns the first index i for which self[i] == value.

This is equivalent to the Python expression self.index(value).

Source

fn iter(&self) -> BoundListIterator<'py>

Returns an iterator over this list’s items.

Source

fn sort(&self) -> PyResult<()>

Sorts the list in-place. Equivalent to the Python expression l.sort().

Source

fn reverse(&self) -> PyResult<()>

Reverses the list in-place. Equivalent to the Python expression l.reverse().

Source

fn to_tuple(&self) -> Bound<'py, PyTuple>

Return a new tuple containing the contents of the list; equivalent to the Python expression tuple(list).

This method is equivalent to self.as_sequence().to_tuple() and faster than PyTuple::new(py, this_list).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'py> PyListMethods<'py> for Bound<'py, PyList>