3 ms·
A frozen dictionary would be very welcome. You can already do something similar using MappingProxyType [0] from types import MappingProxyType d = {}
by polyrand 10mo ago
A frozen dictionary would be very welcome. You can already do something similar using MappingProxyType [0]
from types import MappingProxyType
d = {}
d["a"] = 1
d["b"] = 2
print(d)
frozen = MappingProxyType(d)
print(frozen["a"])
# Error:
frozen["b"] = "new"
[0]: https://docs.python.org/3/library/types.html#types.MappingProxyType https://docs.python.org/3/library/types.html#types.MappingPr...
- zahlman 10mo ago> You can already do something similar Only if you deny access to the underlying real dict.
- ali_m 10mo agoYes, this only prevents the callee from mutating it, it can't provide a strong guarantee that the underlying mapping won't be changed upstream (and hence MappingProxyType can't be washable).