constantdict API reference¶
- 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. Thisconstantdictis 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
constantdictas 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})
A
constantdictmutationcan 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})
Methods that return a modified copy of a
constantdict- set(key: K, value: Any) constantdict[K, V][source]¶
Return a new
constantdictwith the item at key set to val.
- setdefault(key: K, default: V | None = None) constantdict[K, V][source]¶
Return a new
constantdictwith 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
constantdictwithout the item at key.Raise a
KeyErrorif key is not present.
- update(other: Mapping[K, V] | SupportsKeysAndGetItem[K, V] | type[_NotProvided] = <class 'constantdict._NotProvided'>, **kwargs: Any) constantdict[K, V][source]¶
Return a new
constantdictwith 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 # unchanged constantdict({'a': 1, 'b': 2})
- discard(key: K) constantdict[K, V][source]¶
Return a new
constantdictwithout the item at the given key.Return a reference to itself if the key is not present.
Note
Based on the
pyrsistent.PMapAPI.
Deleted methods compared to
dictThese methods raise an
AttributeErrorwhen called.
- class constantdict.constantdictmutation[source]¶
A mutable dictionary that can be converted back to a
constantdictwithout copying. This class behaves exactly like adict, except for the addition 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})
- class constantdict.constantdictuncachedhash[source]¶
A
constantdictthat 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. This class behaves exactly like aconstantdictin all other respects.
- class constantdict.constantdictuncachedhashmutation[source]¶
A mutable dictionary that can be converted back to a
constantdictuncachedhashwithout copying. This class behaves exactly like aconstantdictmutationin all other respects.
Type classes¶
- class constantdict.K¶
A type representing a key in a
constantdict. Must be hashable.
- class constantdict.V¶
A (covariant) type representing a value in a
constantdict.
References¶
(This is mainly here because the documentation tool is confused.)
- class SupportsKeysAndGetItem¶
As the name suggests.