Jul-07-2021, 12:08 AM
we have a couple immutable types. why no immutable dictionaries? yeah, yeah we have named tuples. but those are awkward to create. it should be as simple as creating a literal dictionary.
types.MappingProxyType()
to create a read-only view of an existing dictionary.Guido Van Rossum Wrote:On the other hand, exposing the existing read-only dict proxy as a built-in type sounds good to me. (It would need to be changed to allow calling the constructor.)
>>> import types >>> d = types.MappingProxyType({'foo': 1, 'bar': 10, 'spam': 100}) >>> >>> d mappingproxy({'foo': 1, 'bar': 10, 'spam': 100}) >>> d['bar'] 10 >>> d['bar'] = 3 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'mappingproxy' object does not support item assignment