iterator? way of generating all possible combinations?
akameswaran at gmail.com
akameswaran at gmail.com
Tue May 30 17:34:07 EDT 2006
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
> scott.daniels at acm.org
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
More information about the Python-list
mailing list