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.
| 220 | |
| 221 | |
| 222 | class 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 | |
| 259 | class DictWrapper(dict): |
no outgoing calls