Scott David Daniels wrote: > Sorry, "re-iterables". A file re-iterable is: > > class FileReIterable(object): > def __init__(self, file): > if isinstance(file, basestring): > self.file = open(file, 'rU') > else: > self.file = file > def __iter__(self): > self.file.seek(0) > return iter(self.file) > > This works if-and-only-if it is only in use once at a time. > If you have multiple simultaneous accesses, you need to do > something like: > > class FileReIterable2(object): > def __init__(self, file): > if isinstance(file, basestring): > self.file = open(file, 'rU') > else: > self.file = file > def __iter__(self): > self.file.seek(0) > for line in self.file: > nextpos = self.file.tell() > yield line > self.file.seek(nextpos) > > --Scott David Daniels > [EMAIL PROTECTED]
Since I was doing this as a self education excercise. When you say is in use once and only once, you mean I can only use a single instance of the class? I haven't even tested these yet, but I am very curious about that statement. Why is it necessary to return a generator object in the second example? If it's a real long explanation, feel free to point me to some relvant texts. My practical problem was solved about 10 posts ago... but I am still trying to understand the behavior. Thank you for you time. AK -- http://mail.python.org/mailman/listinfo/python-list