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
| 4 | |
| 5 | # from jaraco.collections 3.3 |
| 6 | class 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 | |
| 28 | class Pair(typing.NamedTuple): |
no outgoing calls
no test coverage detected
searching dependent graphs…