__getitem__と__setitem__を定義すると、リストみたいに[]を使って要素を参照したり代入したりできます。 class Foo: def __init__(self, filename): self.file = file(filename, 'r+') def __setitem__(self, index, value): self.file.seek(index) self.file.write(value) def __getitem__(self, index): self.file.seek(index) return self.file.read(1) f = Foo('list.txt') a = 'apple' for i in range(len(a)): f[i] = a[i] for i in range(len(a)): print f[i]実行結
![2006-10-05](https://cdn-ak-scissors.b.st-hatena.com/image/square/06a15c64ba0ceec233d86d71001ebb29a9dcbf5d/height=288;version=1;width=512/https%3A%2F%2Fcdn.blog.st-hatena.com%2Fimages%2Ftheme%2Fog-image-1500.png)