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

Victor Stinner victor.stinner at gmail.com
Tue Feb 28 18:41:37 CET 2012


 * frozendict values must be immutable, as dict keys Why?  That may be useful, but an immutable dict whose values might mutate is also useful; by forcing that choice, it starts to feel too specialized for a builtin.

Hum, I realized that calling hash(my_frozendict) on a frozendict instance is enough to check if a frozendict only contains immutable objects. And it is also possible to check manually that values are immutable before creating the frozendict.

I also prefer to not check for immutability because it does simplify the code :-)

$ diffstat frozendict-3.patch Include/dictobject.h | 9 + Lib/collections/abc.py | 1 Lib/test/test_dict.py | 59 +++++++++++ Objects/dictobject.c | 256 ++++++++++++++++++++++++++++++++++++++++++------- Objects/object.c | 3 Python/bltinmodule.c | 1 6 files changed, 295 insertions(+), 34 deletions(-)

The patch is quite small to add a new builtin type. That's because most of the code is shared with the builtin dict type. (But the patch doesn't include the documentation, it didn't write it yet.)

Victor -------------- next part -------------- A non-text attachment was scrubbed... Name: frozendict-3.patch Type: text/x-patch Size: 20979 bytes Desc: not available URL: <http://mail.python.org/pipermail/python-dev/attachments/20120228/86eda61d/attachment-0001.bin>



More information about the Python-Dev mailing list