Edited by Gribouillis
Edited by Gribouillis
class ConstSequence(object): """Immutable wrapper around a sequence type instance.""" _SliceType = type(slice(None)) # <-- this should work for all versions of python def __init__(self, seq): if isinstance(seq, ConstSequence): seq = seq._adaptee self._adaptee = seq def __getitem__(self, key): if isinstance(key, self._SliceType): return ConstSequence(self._adaptee[key]) else: return self._adaptee[key] def __len__(self): return len(self._adaptee) def __contains__(self, key): return key in self._adaptee def __iter__(self): return (x for x in self._adaptee) def __reversed__(self): return (x for x in reversed(self._adaptee)) def __str__(self): return 'ConstSequence(%r)' % self._adaptee __repr__ = __str__ if __name__ == "__main__": # example use class A(object): """Class A stores a list internally, but exposes a read-only version of this list. """ def __init__(self): self._items = list() self.items = ConstSequence(self._items) def add_string(self, s): "Methods in class A can alter the list's content" self._items.extend(s) a = A() a.add_string("hello") print a.items[1] # prints 'e' : client code can read the list a.items[1] = "z" # raises TypeError : client code can't alter the list's content
Edited by Gribouillis: n/a
Are you able to help answer this sponsored question?
Questions asked by members who have earned a lot of community kudos are featured in order to give back and encourage quality replies.