constantdict¶
- class constantdict.constantdict[source]¶
An immutable dictionary that does not allow modifications after creation. This class behaves mostly like a
dict
, but with the following differences.Additional methods compared to
dict
:- __hash__() int [source]¶
Return a hash of this
constantdict
. Thisconstantdict
is hashable if all of its keys and values are hashable. Once computed, the hash is cached.
- mutate() constantdictmutation[K, V] [source]¶
Return a mutable copy of this
constantdict
as aconstantdictmutation
.Run
constantdictmutation.finish()
to convert back to an immutableconstantdict
.Note
Based on the immutables.Map API.
>>> cd = constantdict(a=1, b=2) >>> cd_mut = cd.mutate() >>> cd_mut["a"] = 10 >>> del cd_mut["b"] >>> cd_new = cd_mut.finish() >>> cd_new constantdict({'a': 10}) >>> cd # unchanged constantdict({'a': 1, 'b': 2})
Methods that return a modified copy of the
constantdict
:- set(key: K, value: Any) constantdict[K, V] [source]¶
Return a new
constantdict
with the item at key set to val.
- setdefault(key: K, default: V | None = None) constantdict[K, V] [source]¶
Return a new
constantdict
with the item at key set to default if key is not in the dictionary.Return a reference to itself if key is present.
Note
Based on the frozendict API.
- delete(key: K) constantdict[K, V] [source]¶
Return a new
constantdict
without the item at key.Raise a
KeyError
if key is not present.
- update(other: dict[K, V]) constantdict[K, V] [source]¶
Return a new
constantdict
with updated items from other.Note
In contrast to
dict.update()
, this method does not modify the originalconstantdict
, but creates a new, updated copy.>>> cd = constantdict(a=1, b=2) >>> cd_new = cd.update({"a": 10, "c": 3}) >>> cd_new constantdict({'a': 10, 'b': 2, 'c': 3}) >>> cd constantdict({'a': 1, 'b': 2})
- discard(key: K) constantdict[K, V] [source]¶
Return a new
constantdict
without the item at the given key.Return a reference to itself if the key is not present.
Note
Based on the
pyrsistent.PMap
API.
Deleted methods compared to
dict
(these raise anAttributeError
when called):
- class constantdict.constantdictmutation[source]¶
A mutable dictionary that can be converted back to a
constantdict
without copying. This class behaves exactly like adict
, except for the additions mentioned below.Additional method compared to
dict
:- finish() constantdict[K, V] [source]¶
Convert this object to an immutable version of itself.
>>> cd_mut = constantdict(a=1, b=2).mutate() >>> cd_mut["a"] = 12 >>> cd = cd_mut.finish() >>> cd constantdict({'a': 12, 'b': 2})
It can also be used as a context manager:
>>> with constantdict(a=1, b=2).mutate() as cd_mut: ... cd_mut["a"] = 10 ... del cd_mut["b"] ... cd_new = cd_mut.finish() >>> cd_new constantdict({'a': 10})
- class constantdict.constantdictuncachedhash[source]¶
A
constantdict
that does not cache its hash value. This is useful when the dictionary contains items that are not immutable and whose hash value might therefore change.- mutate() constantdictuncachedhashmutation[K, V] [source]¶
Return a mutable copy of this
constantdict
as aconstantdictuncachedhashmutation
.Run
constantdictuncachedhashmutation.finish()
to convert back to an immutableconstantdict
.
- class constantdict.constantdictuncachedhashmutation[source]¶
A mutable dictionary that can be converted back to a
constantdictuncachedhash
without copying. This class behaves exactly like adict
, except for one additional method mentioned below.Additional method compared to
dict
:- finish() constantdictuncachedhash[K, V] [source]¶
Convert this object to an immutable version of itself.
Type classes¶
- class constantdict.K¶
A type representing a key in a
constantdict
. Must be hashable.
- class constantdict.V¶
A type representing a value in a
constantdict
.