PEP 416: add a section about dictproxy, add more links to Python recipes

This commit is contained in:
Victor Stinner 2012-03-14 13:55:31 +01:00
parent 40a8590b14
commit d611d41bfe
1 changed files with 30 additions and 2 deletions

View File

@ -108,15 +108,43 @@ hash(frozendict) raises a TypeError if a value is not hashable. Freezing an
object is not the purpose of this PEP.
Alternative: dictproxy
======================
Python has a builtin dictproxy type used by type.__dict__ getter descriptor.
This type is not public. dictproxy is a read-only view of a dictionary, but it
is not read-only mapping. If a dictionary is modified, the dictproxy is also
modified.
dictproxy can be used using ctypes and the Python C API, see for example the
`make dictproxy object via ctypes.pythonapi and type() (Python recipe 576540)`_
by Ikkei Shimomura. The recipe contains a test checking that a dictproxy is
"mutable" (modify the dictionary linked to the dictproxy).
However dictproxy can be useful in some cases, where its mutable property is
not an issue, to avoid a copy of the dictionary.
Links
=====
* `Issue #14162: PEP 416: Add a builtin frozendict type
<http://bugs.python.org/issue14162>`_
* PEP 412: Key-Sharing Dictionary
(`issue #13903 <http://bugs.python.org/issue13903>`_)
* PEP 351: The freeze protocol
* `The case for immutable dictionaries; and the central misunderstanding of PEP 351 <http://www.cs.toronto.edu/~tijmen/programming/immutableDictionaries.html>`_
* `Frozen dictionaries (Python recipe 414283) <http://code.activestate.com/recipes/414283-frozen-dictionaries/>`_
by Oren Tirosh
* `Frozen dictionaries (Python recipe 414283) <http://code.activestate.com/recipes/414283/>`_
by Oren Tirosh. Blacklist approach: inherit from dict and override write
methods to raise an exception. It is not truly read-only: it is still
possible to call dict methods on such "frozen dictionary" to modify it.
* `Implementing an Immutable Dictionary (Python recipe 498072) <http://code.activestate.com/recipes/498072/>`_
by Aristotelis Mikropoulos. Similar to frozendict except that it is not
truly read-only. It is possible to access to this private internal dict.
It does not implement __hash__ and has an implementation issue: it is
possible to call again __init__() to modify the mapping.
* `make dictproxy object via ctypes.pythonapi and type() (Python recipe
576540) <http://code.activestate.com/recipes/576540/>`_ by Ikkei Shimomura.
* Python security modules implementing read-only object proxies using a C
extension: