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: ToPyObject;
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: ToPyObject;
fn insert<I>(&self, index: usize, item: I) -> PyResult<()>
where I: ToPyObject;
fn contains<V>(&self, value: V) -> PyResult<bool>
where V: ToPyObject;
fn index<V>(&self, value: V) -> PyResult<usize>
where V: ToPyObject;
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§
Sourcefn as_sequence(&self) -> &Bound<'py, PySequence>
fn as_sequence(&self) -> &Bound<'py, PySequence>
Returns self
cast as a PySequence
.
Sourcefn into_sequence(self) -> Bound<'py, PySequence>
fn into_sequence(self) -> Bound<'py, PySequence>
Returns self
cast as a PySequence
.
Sourcefn get_item(&self, index: usize) -> PyResult<Bound<'py, PyAny>>
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_bound(py, [2, 3, 5, 7]);
let obj = list.get_item(0);
assert_eq!(obj.unwrap().extract::<i32>().unwrap(), 2);
});
Sourceunsafe fn get_item_unchecked(&self, index: usize) -> Bound<'py, PyAny>
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.
Sourcefn get_slice(&self, low: usize, high: usize) -> Bound<'py, PyList>
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()
.
Sourcefn set_item<I>(&self, index: usize, item: I) -> PyResult<()>where
I: ToPyObject,
fn set_item<I>(&self, index: usize, item: I) -> PyResult<()>where
I: ToPyObject,
Sets the item at the specified index.
Raises IndexError
if the index is out of range.
Sourcefn del_item(&self, index: usize) -> PyResult<()>
fn del_item(&self, index: usize) -> PyResult<()>
Deletes the index
th element of self.
This is equivalent to the Python statement del self[i]
.
Sourcefn set_slice(
&self,
low: usize,
high: usize,
seq: &Bound<'_, PyAny>,
) -> PyResult<()>
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
.
Sourcefn del_slice(&self, low: usize, high: usize) -> PyResult<()>
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]
.
Sourcefn append<I>(&self, item: I) -> PyResult<()>where
I: ToPyObject,
fn append<I>(&self, item: I) -> PyResult<()>where
I: ToPyObject,
Appends an item to the list.
Sourcefn insert<I>(&self, index: usize, item: I) -> PyResult<()>where
I: ToPyObject,
fn insert<I>(&self, index: usize, item: I) -> PyResult<()>where
I: ToPyObject,
Inserts an item at the specified index.
If index >= self.len()
, inserts at the end.
Sourcefn contains<V>(&self, value: V) -> PyResult<bool>where
V: ToPyObject,
fn contains<V>(&self, value: V) -> PyResult<bool>where
V: ToPyObject,
Determines if self contains value
.
This is equivalent to the Python expression value in self
.
Sourcefn index<V>(&self, value: V) -> PyResult<usize>where
V: ToPyObject,
fn index<V>(&self, value: V) -> PyResult<usize>where
V: ToPyObject,
Returns the first index i
for which self[i] == value
.
This is equivalent to the Python expression self.index(value)
.
Sourcefn iter(&self) -> BoundListIterator<'py> ⓘ
fn iter(&self) -> BoundListIterator<'py> ⓘ
Returns an iterator over this list’s items.
Sourcefn sort(&self) -> PyResult<()>
fn sort(&self) -> PyResult<()>
Sorts the list in-place. Equivalent to the Python expression l.sort()
.
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.