For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).
OwnedKwargsDict
struct OwnedKwargsDict[V: Movable & ImplicitlyDestructible]
Container used to pass owned variadic keyword arguments to functions.
This type mimics the interface of a dictionary with String keys, and
should be usable more-or-less like a dictionary. Notably, however, this type
should not be instantiated directly by users.
Parameters
- V (
Movable&ImplicitlyDestructible): The value type of the dictionary. Must beMovable & ImplicitlyDestructible; copy-requiring operations are conditionally available viawhere conforms_to(V, Copyable).
Implemented traits
AnyType,
Copyable,
Defaultable,
ImplicitlyDestructible,
Iterable,
Movable,
Sized
comptime members
IteratorType
comptime IteratorType[iterable_mut: Bool, //, iterable_origin: Origin[mut=iterable_mut]] = _DictKeyIter[String, V(ImplicitlyDestructible & Copyable & Movable), Fnv1a, iterable_origin]
The iterator type for this dictionary.
Parameters
- iterable_mut (
Bool): Whether the iterable is mutable. - iterable_origin (
Origin[mut=iterable_mut]): The origin of the iterable.
key_type
comptime key_type = String
The key type for this dictionary (always String).
Methods
__init__
def __init__(out self)
Initialize an empty keyword dictionary.
__getitem__
def __getitem__(ref self, ref key: String) -> ref[self_is_mut._dict] V
Retrieve a value out of the keyword dictionary.
Args:
- key (
String): The key to retrieve.
Returns:
ref[self_is_mut._dict] V: The value associated with the key, if it's present.
Raises:
DictKeyError if the key isn't present.
__setitem__
def __setitem__(mut self, key: String, var value: V)
Set a value in the keyword dictionary by key.
Args:
- key (
String): The key to associate with the specified value. - value (
V): The data to store in the dictionary.
__contains__
def __contains__(self, key: String) -> Bool
Check if a given key is in the keyword dictionary or not.
Args:
- key (
String): The key to check.
Returns:
Bool: True if there key exists in the keyword dictionary, False
otherwise.
__len__
def __len__(self) -> Int
The number of elements currently stored in the keyword dictionary.
Returns:
Int: The number of elements currently stored in the keyword dictionary.
find
def find(self, key: String) -> Optional[V] where conforms_to(V, AnyType & Copyable & Movable)
Find a value in the keyword dictionary by key.
Args:
- key (
String): The key to search for in the dictionary.
Returns:
Optional[V]: An optional value containing a copy of the value if it was present,
otherwise an empty Optional.
pop
def pop(mut self, key: String, var default: V) -> V
Remove a value from the dictionary by key.
Args:
- key (
String): The key to remove from the dictionary. - default (
V): A default value to return if the key was not found instead of raising.
Returns:
V: The value associated with the key, if it was in the dictionary.
If it wasn't, return the provided default value instead.
def pop(mut self, ref key: String) -> V
Remove a value from the dictionary by key.
Args:
- key (
String): The key to remove from the dictionary.
Returns:
V: The value associated with the key, if it was in the dictionary.
Raises otherwise.
Raises:
DictKeyError if the key was not present in the dictionary.
__iter__
def __iter__(ref self) -> _DictKeyIter[String, V(ImplicitlyDestructible & Copyable & Movable), Fnv1a, origin_of(self)]
Iterate over the keyword dict's keys as immutable references.
Returns:
_DictKeyIter[String, V(ImplicitlyDestructible & Copyable & Movable), Fnv1a, origin_of(self)]: An iterator of immutable references to the dictionary keys.
keys
def keys(ref self) -> _DictKeyIter[String, V(ImplicitlyDestructible & Copyable & Movable), Fnv1a, origin_of(self._dict)]
Iterate over the keyword dict's keys as immutable references.
Returns:
_DictKeyIter[String, V(ImplicitlyDestructible & Copyable & Movable), Fnv1a, origin_of(self._dict)]: An iterator of immutable references to the dictionary keys.
values
def values(ref self) -> _DictValueIter[String, V(ImplicitlyDestructible & Copyable & Movable), Fnv1a, origin_of(self._dict)]
Iterate over the keyword dict's values as references.
Returns:
_DictValueIter[String, V(ImplicitlyDestructible & Copyable & Movable), Fnv1a, origin_of(self._dict)]: An iterator of references to the dictionary values.
items
def items(ref self) -> _DictEntryIter[String, V(ImplicitlyDestructible & Copyable & Movable), Fnv1a, origin_of(self._dict)]
Iterate over the keyword dictionary's entries as immutable references.
Examples:
var my_dict = Dict[String, Int]()
my_dict["a"] = 1
my_dict["b"] = 2
for e in my_dict.items():
print(e.key, e.value)
Notes: These can't yet be unpacked like Python dict items, but you can access the key and value as attributes.
Returns:
_DictEntryIter[String, V(ImplicitlyDestructible & Copyable & Movable), Fnv1a, origin_of(self._dict)]: An iterator of immutable references to the dictionary entries.