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§
Sourcefn copy(&self) -> PyResult<Bound<'py, PyDict>>
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()
.
Sourcefn len(&self) -> usize
fn len(&self) -> usize
Return the number of items in the dictionary.
This is equivalent to the Python expression len(self)
.
Sourcefn contains<K>(&self, key: K) -> PyResult<bool>where
K: ToPyObject,
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
.
Sourcefn get_item<K>(&self, key: K) -> PyResult<Option<Bound<'py, PyAny>>>where
K: ToPyObject,
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
.
Sourcefn set_item<K, V>(&self, key: K, value: V) -> PyResult<()>where
K: ToPyObject,
V: ToPyObject,
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
.
Sourcefn del_item<K>(&self, key: K) -> PyResult<()>where
K: ToPyObject,
fn del_item<K>(&self, key: K) -> PyResult<()>where
K: ToPyObject,
Deletes an item.
This is equivalent to the Python statement del self[key]
.
Sourcefn keys(&self) -> Bound<'py, PyList>
fn keys(&self) -> Bound<'py, PyList>
Returns a list of dict keys.
This is equivalent to the Python expression list(dict.keys())
.
Sourcefn values(&self) -> Bound<'py, PyList>
fn values(&self) -> Bound<'py, PyList>
Returns a list of dict values.
This is equivalent to the Python expression list(dict.values())
.
Sourcefn items(&self) -> Bound<'py, PyList>
fn items(&self) -> Bound<'py, PyList>
Returns a list of dict items.
This is equivalent to the Python expression list(dict.items())
.
Sourcefn iter(&self) -> BoundDictIterator<'py> ⓘ
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.
Sourcefn as_mapping(&self) -> &Bound<'py, PyMapping>
fn as_mapping(&self) -> &Bound<'py, PyMapping>
Returns self
cast as a PyMapping
.
Sourcefn into_mapping(self) -> Bound<'py, PyMapping>
fn into_mapping(self) -> Bound<'py, PyMapping>
Returns self
cast as a PyMapping
.
Sourcefn update(&self, other: &Bound<'_, PyMapping>) -> PyResult<()>
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.
Sourcefn update_if_missing(&self, other: &Bound<'_, PyMapping>) -> PyResult<()>
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
.
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.