Skip to main content
Log in

Mojo struct

OwnedKwargsDict

struct OwnedKwargsDict[V: CollectionElement]

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, UnknownDestructibility

Methods

__init__

__init__(out self)

Initialize an empty keyword dictionary.

__init__(out self, *, other: Self)

Copy an existing keyword dictionary.

Args:

  • other (Self): The existing keyword dictionary.

__copyinit__

__copyinit__(out self, existing: Self)

Copy an existing keyword dictionary.

Args:

  • existing (Self): The existing keyword dictionary.

__moveinit__

__moveinit__(out 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, 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__(mut 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, 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) -> 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, 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(mut 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(mut 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) -> _DictKeyIter[String, V, self_is_origin._dict]

Iterate over the keyword dict's keys as immutable references.

Returns:

An iterator of immutable references to the dictionary keys.

keys

keys(ref self) -> _DictKeyIter[String, V, self_is_origin._dict]

Iterate over the keyword dict's keys as immutable references.

Returns:

An iterator of immutable references to the dictionary keys.

values

values(ref self) -> _DictValueIter[String, V, self_is_origin._dict]

Iterate over the keyword dict's values as references.

Returns:

An iterator of references to the dictionary values.

items

items(ref self) -> _DictEntryIter[String, V, self_is_origin._dict]

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.

from collections import Dict

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)
from collections import Dict

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)

Returns:

An iterator of immutable references to the dictionary entries.