←back to thread

191 points jwilk | 2 comments | | HN request time: 0.405s | source
Show context
polyrand ◴[] No.46230124[source]
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.MappingPr...
replies(1): >>46231092 #
1. zahlman ◴[] No.46231092[source]
> You can already do something similar

Only if you deny access to the underlying real dict.

replies(1): >>46231368 #
2. ali_m ◴[] No.46231368[source]
Yes, 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).