* Rephrase the first Rationale paragraph
 * Add 2 more use cases: cache and sandbox
 * Fix a typo
This commit is contained in:
Victor Stinner 2012-03-01 13:06:07 +01:00
parent e413ea93b1
commit d0beba79d5
1 changed files with 8 additions and 4 deletions

View File

@ -19,16 +19,20 @@ Add a new frozendict builtin type.
Rationale
=========
A frozendict mapping cannot be changed, but its values can be mutable (not
hashable). A frozendict is hashable and so immutable if all values are hashable
(immutable).
A frozendict is a read-only mapping: a key cannot be added nor removed, and a
key is always mapped to the same value. However, frozendict values can be
mutable (not hashable). A frozendict is hashable and so immutable if and only
if all values are hashable (immutable).
Use cases of frozendict:
* frozendict can be used to implement a cache
* hashable frozendict can be used as a key of a mapping or as a member of set
* frozendict helps optimization because the mapping is constant
* frozendict avoids the need of a lock when the frozendict is shared
by multiple threads or processes, especially hashable frozendict
* frozendict helps to implement a security sandbox with read-only objects,
e.g. freeze __builtins__ mapping
Constraints
@ -47,7 +51,7 @@ Implementation
"Py_hash_t hash;" field
* frozendict.__hash__() is implemented using hash(frozenset(self.items())) and
caches the result in its private hash attribute
* Register frozendict has a collections.abc.Mapping
* Register frozendict as a collections.abc.Mapping
* frozendict can be used with PyDict_GetItem(), but PyDict_SetItem() and
PyDict_DelItem() raise a TypeError