MCPcopy
hub / github.com/django/django / ImmutableList

Class ImmutableList

django/utils/datastructures.py:222–256  ·  view source on GitHub ↗

A tuple-like object that raises useful errors when it is asked to mutate. Example:: >>> a = ImmutableList(range(5), warning="You cannot mutate this.") >>> a[3] = '4' Traceback (most recent call last): ... AttributeError: You cannot mutate this.

Source from the content-addressed store, hash-verified

220
221
222class ImmutableList(tuple):
223 """
224 A tuple-like object that raises useful errors when it is asked to mutate.
225
226 Example::
227
228 >>> a = ImmutableList(range(5), warning="You cannot mutate this.")
229 >>> a[3] = '4'
230 Traceback (most recent call last):
231 ...
232 AttributeError: You cannot mutate this.
233 """
234
235 def __new__(cls, *args, warning="ImmutableList object is immutable.", **kwargs):
236 self = tuple.__new__(cls, *args, **kwargs)
237 self.warning = warning
238 return self
239
240 def complain(self, *args, **kwargs):
241 raise AttributeError(self.warning)
242
243 # All list mutation functions complain.
244 __delitem__ = complain
245 __delslice__ = complain
246 __iadd__ = complain
247 __imul__ = complain
248 __setitem__ = complain
249 __setslice__ = complain
250 append = complain
251 extend = complain
252 insert = complain
253 pop = complain
254 remove = complain
255 sort = complain
256 reverse = complain
257
258
259class DictWrapper(dict):

Callers 4

parse_file_uploadMethod · 0.90
test_sortMethod · 0.90
test_custom_warningMethod · 0.90

Calls

no outgoing calls

Tested by 2

test_sortMethod · 0.72
test_custom_warningMethod · 0.72