[Python-Dev] Add a frozendict builtin type (original) (raw)

Victor Stinner victor.stinner at haypocalc.com
Mon Feb 27 19:53:27 CET 2012


Rationale

A frozendict type is a common request from users and there are various implementations. There are two main Python implementations:

The blacklist implementation has a major issue: it is still possible to call write methods of the dict class (e.g. dict.set(my_frozendict, key, value)).

The whitelist implementation has an issue: frozendict and dict are not "compatible", dict is not a subclass of frozendict (and frozendict is not a subclass of dict).

I propose to add a new frozendict builtin type and make dict type inherits from it. frozendict would not have methods to modify its content and values must be immutable.

Constraints

Implementation

Attached patch is a work-in-progress implementation.

TODO

--

Examples of frozendict implementations:

http://bob.pythonmac.org/archives/2005/03/04/frozendict/ http://code.activestate.com/recipes/498072-implementing-an-immutable-dictionary/ http://code.activestate.com/recipes/414283-frozen-dictionaries/ http://corebio.googlecode.com/svn/trunk/apidocs/corebio.utils.frozendict-class.html http://code.google.com/p/lingospot/source/browse/trunk/frozendict/frozendict.py http://cmssdt.cern.ch/SDT/doxygen/CMSSW_4_4_2/doc/html/d6/d2f/classfrozendict_1_1frozendict.html

See also the recent discussion on python-list:

http://mail.python.org/pipermail/python-list/2012-February/1287658.html

--

See also the PEP 351.

Victor -------------- next part -------------- A non-text attachment was scrubbed... Name: frozendict.patch Type: text/x-patch Size: 24014 bytes Desc: not available URL: <http://mail.python.org/pipermail/python-dev/attachments/20120227/ff1984ed/attachment.bin>



More information about the Python-Dev mailing list