In [ 5 ]: from types import MappingProxyType In [ 6 ]: d = { 1 : 'a' } In [ 7 ]: d_proxy = MappingProxyType ( d ) In [ 8 ]: d_proxy [ 1 ] Out [ 8 ]: 'a' In [ 9 ]: d_proxy Out [ 9 ]: mappingproxy ({ 1 : 'a' }) In [ 10 ]: d_proxy [ 2 ] - - -
In [6]: d = {1:'a'}
In [7]: d_proxy = MappingProxyType(d)
In [8]: d_proxy[1]
Out[8]: 'a'
In [9]: d_proxy
Out[9]: mappingproxy({1: 'a'})
In [10]: d_proxy[2]
------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-10-6f9dce4cd735> in <module>
----> 1 d_proxy[2]
KeyError: 2
In [11]: d_proxy[2] = 'x'
------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-bc17a9a62754> in <module>
----> 1 d_proxy[2] = 'x'
TypeError: 'mappingproxy' object does not support item assignment
In [12]: d[2] = 'x'
In [13]: d_proxy
Out[13]: mappingproxy({1: 'a', 2: 'x'})