iterators and generators, is this the Python Way???

Michael Schneider michaels at one.net
Thu Sep 12 10:08:25 CEST 2002


Just,

Thank you very much, I like this approach.

I was under the mistaken impression what the
iterator needed to do.

Mike

Just wrote:

>In article <3D7FF921.2040305 at one.net>,
> Michael Schneider <michaels at one.net> wrote:
>
>>class FastFile:
>>    def __init__(self, filename):
>>        """ Read in entire file into memory"""
>>        f = open(filename, 'r')
>>        fileContents = f.read();
>>        self.lines = fileContents.split('\n')
>>        f.close()
>>   
>>    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...
>
>Just
>




More information about the Python-list mailing list