Mojo struct
OwnedKwargsDict
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 (
CollectionElement
): The value type of the dictionary. Currently must be CollectionElement.
Aliases
key_type = String
:
Implemented traits
AnyType
,
CollectionElement
,
CollectionElementNew
,
Copyable
,
ExplicitlyCopyable
,
Movable
,
Sized
Methods
__init__
__init__(inout self: Self)
Initialize an empty keyword dictionary.
__init__(inout self: Self, *, other: Self)
Copy an existing keyword dictionary.
Args:
- other (
Self
): The existing keyword dictionary.
__copyinit__
__copyinit__(inout self: Self, existing: Self)
Copy an existing keyword dictionary.
Args:
- existing (
Self
): The existing keyword dictionary.
__moveinit__
__moveinit__(inout self: Self, owned existing: Self)
Move data of an existing keyword dictionary into a new one.
Args:
- existing (
Self
): The existing keyword dictionary.
__getitem__
__getitem__(self: Self, key: String) -> V
Retrieve a value out of the keyword dictionary.
Args:
- key (
String
): The key to retrieve.
Returns:
The value associated with the key, if it's present.
Raises:
"KeyError" if the key isn't present.
__setitem__
__setitem__(inout self: Self, key: String, 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__
__contains__(self: Self, key: String) -> Bool
Check if a given key is in the keyword dictionary or not.
Args:
- key (
String
): The key to check.
Returns:
True if there key exists in the keyword dictionary, False otherwise.
__len__
__len__(self: Self) -> Int
The number of elements currently stored in the keyword dictionary.
Returns:
The number of elements currently stored in the keyword dictionary.
find
find(self: Self, key: String) -> Optional[V]
Find a value in the keyword dictionary by key.
Args:
- key (
String
): The key to search for in the dictionary.
Returns:
An optional value containing a copy of the value if it was present, otherwise an empty Optional.
pop
pop(inout self: Self, key: String, owned 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:
The value associated with the key, if it was in the dictionary. If it wasn't, return the provided default value instead.
pop(inout self: Self, key: String) -> V
Remove a value from the dictionary by key.
Args:
- key (
String
): The key to remove from the dictionary.
Returns:
The value associated with the key, if it was in the dictionary. Raises otherwise.
Raises:
"KeyError" if the key was not present in the dictionary.
__iter__
__iter__(ref [self_is_lifetime] self: Self) -> _DictKeyIter[$0, String, V, $1._dict, 1]
Iterate over the keyword dict's keys as immutable references.
Returns:
An iterator of immutable references to the dictionary keys.
keys
keys(ref [self_is_lifetime] self: Self) -> _DictKeyIter[$0, String, V, $1._dict, 1]
Iterate over the keyword dict's keys as immutable references.
Returns:
An iterator of immutable references to the dictionary keys.
values
values(ref [self_is_lifetime] self: Self) -> _DictValueIter[$0, String, V, $1._dict, 1]
Iterate over the keyword dict's values as references.
Returns:
An iterator of references to the dictionary values.
items
items(ref [self_is_lifetime] self: Self) -> _DictEntryIter[$0, String, V, $1._dict, 1]
Iterate over the keyword dictionary's entries as immutable references.
These can't yet be unpacked like Python dict items, but you can access the key and value as attributes ie.
for e in dict.items():
print(e[].key, e[].value)
for e in dict.items():
print(e[].key, e[].value)
Returns:
An iterator of immutable references to the dictionary entries.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!