immutable types - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: News and Discussions (https://python-forum.io/forum-31.html) +--- Thread: immutable types (/thread-34201.html) |
immutable types - Skaperen - Jul-07-2021 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. RE: immutable types - Gribouillis - Jul-07-2021 There was a rejected PEP 416 to create a frozendict type. You can find there the arguments why it was rejected. Also note the use of 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.) There is also a frozendict module in Pypi. Could be worth checking. >>> 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 RE: immutable types - Skaperen - Jul-09-2021 in that language i created 3 decades ago, its mapping type also served as sets. it had a way to place a key with no value (and to delete a value while keeping the key in place). every type had mutable and immutable forms. i remember when i first encountered Python, i backed away because str was strictly immutable. now i realize that every use case for a "mutable str" is better handled by a list. |