Trait pyo3::types::PyDictMethods

source ·
pub trait PyDictMethods<'py>: Sealed {
Show 16 methods // Required methods fn copy(&self) -> PyResult<Bound<'py, PyDict>>; fn clear(&self); fn len(&self) -> usize; fn is_empty(&self) -> bool; fn contains<K>(&self, key: K) -> PyResult<bool> where K: ToPyObject; fn get_item<K>(&self, key: K) -> PyResult<Option<Bound<'py, PyAny>>> where K: ToPyObject; fn set_item<K, V>(&self, key: K, value: V) -> PyResult<()> where K: ToPyObject, V: ToPyObject; fn del_item<K>(&self, key: K) -> PyResult<()> where K: ToPyObject; fn keys(&self) -> Bound<'py, PyList>; fn values(&self) -> Bound<'py, PyList>; fn items(&self) -> Bound<'py, PyList>; fn iter(&self) -> BoundDictIterator<'py> ; fn as_mapping(&self) -> &Bound<'py, PyMapping>; fn into_mapping(self) -> Bound<'py, PyMapping>; fn update(&self, other: &Bound<'_, PyMapping>) -> PyResult<()>; fn update_if_missing(&self, other: &Bound<'_, PyMapping>) -> PyResult<()>;
}
Expand description

Implementation of functionality for PyDict.

These methods are defined for the Bound<'py, PyDict> 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 copy(&self) -> PyResult<Bound<'py, PyDict>>

Returns a new dictionary that contains the same key-value pairs as self.

This is equivalent to the Python expression self.copy().

source

fn clear(&self)

Empties an existing dictionary of all key-value pairs.

source

fn len(&self) -> usize

Return the number of items in the dictionary.

This is equivalent to the Python expression len(self).

source

fn is_empty(&self) -> bool

Checks if the dict is empty, i.e. len(self) == 0.

source

fn contains<K>(&self, key: K) -> PyResult<bool>
where K: ToPyObject,

Determines if the dictionary contains the specified key.

This is equivalent to the Python expression key in self.

source

fn get_item<K>(&self, key: K) -> PyResult<Option<Bound<'py, PyAny>>>
where K: ToPyObject,

Gets an item from the dictionary.

Returns None if the item is not present, or if an error occurs.

To get a KeyError for non-existing keys, use PyAny::get_item.

source

fn set_item<K, V>(&self, key: K, value: V) -> PyResult<()>
where K: ToPyObject, V: ToPyObject,

Sets an item value.

This is equivalent to the Python statement self[key] = value.

source

fn del_item<K>(&self, key: K) -> PyResult<()>
where K: ToPyObject,

Deletes an item.

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

source

fn keys(&self) -> Bound<'py, PyList>

Returns a list of dict keys.

This is equivalent to the Python expression list(dict.keys()).

source

fn values(&self) -> Bound<'py, PyList>

Returns a list of dict values.

This is equivalent to the Python expression list(dict.values()).

source

fn items(&self) -> Bound<'py, PyList>

Returns a list of dict items.

This is equivalent to the Python expression list(dict.items()).

source

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

Returns an iterator of (key, value) pairs in this dictionary.

§Panics

If PyO3 detects that the dictionary is mutated during iteration, it will panic. It is allowed to modify values as you iterate over the dictionary, but only so long as the set of keys does not change.

source

fn as_mapping(&self) -> &Bound<'py, PyMapping>

Returns self cast as a PyMapping.

source

fn into_mapping(self) -> Bound<'py, PyMapping>

Returns self cast as a PyMapping.

source

fn update(&self, other: &Bound<'_, PyMapping>) -> PyResult<()>

Update this dictionary with the key/value pairs from another.

This is equivalent to the Python expression self.update(other). If other is a PyDict, you may want to use self.update(other.as_mapping()), note: PyDict::as_mapping is a zero-cost conversion.

source

fn update_if_missing(&self, other: &Bound<'_, PyMapping>) -> PyResult<()>

Add key/value pairs from another dictionary to this one only when they do not exist in this.

This is equivalent to the Python expression self.update({k: v for k, v in other.items() if k not in self}). If other is a PyDict, you may want to use self.update_if_missing(other.as_mapping()), note: PyDict::as_mapping is a zero-cost conversion.

This method uses PyDict_Merge internally, so should have the same performance as update.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<'py> PyDictMethods<'py> for Bound<'py, PyDict>