PEP 416
* Rephrase the first Rationale paragraph * Add 2 more use cases: cache and sandbox * Fix a typo
This commit is contained in:
parent
e413ea93b1
commit
d0beba79d5
12
pep-0416.txt
12
pep-0416.txt
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue