Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
immutable types
#2
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
Reply


Messages In This Thread
immutable types - by Skaperen - Jul-07-2021, 12:08 AM
RE: immutable types - by Gribouillis - Jul-07-2021, 06:10 AM
RE: immutable types - by Skaperen - Jul-09-2021, 01:00 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Are tuples immutable? python_user_n 13 5,843 Jan-07-2019, 01:26 PM
Last Post: python_user_n

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020