MCPcopy Index your code
hub / github.com/python/cpython / FreezableDefaultDict

Class FreezableDefaultDict

Lib/importlib/metadata/_collections.py:6–25  ·  view source on GitHub ↗

Often it is desirable to prevent the mutation of a default dict after its initial construction, such as to prevent mutation during iteration. >>> dd = FreezableDefaultDict(list) >>> dd[0].append('1') >>> dd.freeze() >>> dd[1] [] >>> len(dd) 1

Source from the content-addressed store, hash-verified

4
5# from jaraco.collections 3.3
6class FreezableDefaultDict(collections.defaultdict):
7 """
8 Often it is desirable to prevent the mutation of
9 a default dict after its initial construction, such
10 as to prevent mutation during iteration.
11
12 >>> dd = FreezableDefaultDict(list)
13 >>> dd[0].append('1')
14 >>> dd.freeze()
15 >>> dd[1]
16 []
17 >>> len(dd)
18 1
19 """
20
21 def __missing__(self, key):
22 return getattr(self, '_frozen', super().__missing__)(key)
23
24 def freeze(self):
25 self._frozen = lambda key: self.default_factory()
26
27
28class Pair(typing.NamedTuple):

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…