Generator not generating

Christopher T King squirrel at WPI.EDU
Thu Jul 22 20:52:01 EDT 2004


On Thu, 22 Jul 2004, Jeff Lowery wrote:

>     def plot_dist(self):
>         file = None;
>         
>         file = nextFile().next()  # call to generator here
> 
> ****
> 
> I trace through this in the debugger and the call to nextFile().next()
> always starts at the beginning of the function (it always returns the
> first file in C:/somedir).

That's because your code restarts the generator each time ;)

When a generator function is called, it returns a new generator object
that starts from the beginning of the function that can then be iterated
over.  To get the effect you want, you should call the generator only
once, and then store its value away for future use:

    def __init__(self, master):
        self.files = nextFile()

    def plot_dist(self):
        file = self.files.next()  # call to generator here

Hope this helps.




More information about the Python-list mailing list