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

Just just at xs4all.nl
Thu Sep 12 03:02:34 EDT 2002


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