readonlydict package#

class readonlydict.HashableMapping[source]#

Bases: Hashable, Mapping[K, V]

Abstract base class for mapping objects that are also hashable.

abstractmethod __getitem__(key)#
abstractmethod __hash__()#

Return hash(self).

abstractmethod __iter__()#
abstractmethod __len__()#
__reversed__ = None#
get(k[, d]) D[k] if k in D, else d.  d defaults to None.#
items() a set-like object providing a view on D's items#
keys() a set-like object providing a view on D's keys#
values() an object providing a view on D's values#
readonlydict.Item#

Type alias of a key-value pair for mapping.

alias of tuple[K, V]

readonlydict.Items#

Type alias of key-value pairs for mapping.

alias of Iterable[tuple[K, V]]

class readonlydict.ReadonlyDict(*args: Any, **kwargs: Any)[source]#

Bases: HashableMapping[K, V]

Drop-in read-only dictionary with 100% typing and runtime compatibility.

  • ReadonlyDict() -> New empty read-only dictionary.

  • ReadonlyDict(mapping) -> New read-only dictionary initialized from a mapping object’s (key, value) pairs.

  • ReadonlyDict(iterable) -> New read-only dictionary initialized as if via: d = {}; for k, v in iterable: d[k] = v.

  • ReadonlyDict(**kwargs) -> New read-only dictionary initialized with the name=value pairs in the keyword argument list. For example: ReadonlyDict(one=1, two=2).

Note

While this dictionary is immutable, calling hash() on it requires that all of its values are also hashable. If the dictionary contains unhashable values (e.g., lists or standard dicts), a TypeError will be raised when the hash is computed.

__getitem__(key: K, /) V[source]#

Return self[key].

__hash__() int[source]#

Return hash(self).

__init__(*args: Any, **kwargs: Any) None[source]#

Initialize self. See help(type(self)) for accurate signature.

__iter__() Iterator[K][source]#

Return iter(self).

__len__() int[source]#

Return len(self).

__or__(value: Mapping[K2, V2], /) ReadonlyDict[K | K2, V | V2][source]#

Return self|value.

__repr__() str[source]#

Return repr(self).

__reversed__() Iterator[K][source]#

Return reversed(self).

__ror__(value: Mapping[K2, V2], /) dict[K | K2, V | V2][source]#

Return value|self.

copy() Self[source]#

Return a shallow copy of self.

classmethod fromkeys(iterable: Iterable[K2], /) ReadonlyDict[K2, None][source]#
classmethod fromkeys(iterable: Iterable[K2], value: V2, /) ReadonlyDict[K2, V2]

Create a new read-only dictionary with keys from iterable and values set to value.

get(k[, d]) D[k] if k in D, else d.  d defaults to None.#
items() a set-like object providing a view on D's items#
keys() a set-like object providing a view on D's keys#
values() an object providing a view on D's values#