As you discovered, __getattr__ does not work this way. If you really want to use list indexing, use __getitem__ and __setitem__ , and forget about getattr() and setattr() . Something like that:
class Lists (object): def __init__(self): self.thelist = [0,0,0] def __getitem__(self, index): return self.thelist[index] def __setitem__(self, index, value): self.thelist[index] = value def __repr__(self): return repr(self.thelist) Ls = Lists() print Ls print Ls[1] Ls[2] = 9 print Ls print Ls[2]
Ethan furman
source share