[Python-ideas] Immutable dictionaries

Stephan Houben stephanh42 at gmail.com
Thu Nov 30 06:35:26 EST 2017

2017-11-30 2:16 GMT+01:00 Rob Cliffe <rob.cliffe at btinternet.com>:

> This is sort of a subset of an idea I once posted on Python-Ideas:
> Dictionaries, sets and lists (etc. ?) could have a mutable flag, and once
> it was set to False you could neither change it back nor change the
> object.  (OK there might be some backdoor hacks, this is Python.)  This
> would avoid having to explain to beginners "Why does Python have lists and
> tuples?" because instead of a tuple, all you need is an immutable list.
> (Or if you prefer, instead of a list, all you need is a mutable tuple.)

Here is a concept implementation of a freeze() function which does
something similar.
Rather than mutating the mutable object to become immutable, it
makes an immutable copy.

>>> freeze({"x": [1,2,{3,4}]})
mappingproxy({'x': (1, 2, frozenset({3, 4}))})



> Rob Cliffe
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171130/5e73034b/attachment-0001.html>

More information about the Python-ideas mailing list