iterators and generators, is this the Python Way???
michaels at one.net
Thu Sep 12 10:08:25 CEST 2002
Thank you very much, I like this approach.
I was under the mistaken impression what the
iterator needed to do.
>In article <3D7FF921.2040305 at one.net>,
> Michael Schneider <michaels at one.net> wrote:
>> def __init__(self, filename):
>> """ Read in entire file into memory"""
>> f = open(filename, 'r')
>> fileContents = f.read();
>> self.lines = fileContents.split('\n')
>> def __iter__(self):
>> return self.fastFileGen()
>> def fastFileGen(self):
>> for line in self.lines:
>> yield line
>I would write the __iter__ like so:
> def __iter__(self):
> for line in self.lines:
> yield line
>Same effect, less code...
More information about the Python-list