Generator not generating

Discussion in 'Python' started by Jeff Lowery, Jul 23, 2004.

  1. Jeff Lowery

    Jeff Lowery Guest

    Hi,

    Although I'm no Python expert, I have written generators in the past
    that have worked like a charm. Don't know why this one doesn't:


    ****
    class App:
    """
    A simple Tk application that plots random lines
    """

    def __init__(self, master):
    # create the main frame
    frame = Frame(master)
    frame.pack()

    # create a canvas within the frame
    self.canvas = Canvas(frame, width=400, height=400)
    self.canvas.pack(side=TOP)

    # create a button below the canvas and within the frame
    # method add_line() is invoked if the button is clicked
    button = Button(frame, text="Plot",
    command=self.plot_dist)
    button.pack(side=BOTTOM)

    def plot_dist(self):
    file = None;

    file = nextFile().next() # call to generator here
    ...

    def nextFile():
    dirPath = 'C:/somedir'
    file = None;

    for filename in os.listdir(dirPath):
    file = open(dirPath + '/' + filename, 'r')
    yield file


    #
    # MAIN
    #
    # Initialize the graphical toolkit
    root = Tk()

    # Create the sample graphical application
    app = App(root)

    # Use in stand-alone programs, not in IDLE
    root.mainloop()

    ****

    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).

    So what stoopid mistake am I overlooking here? I am running v2.3, BTW.
    Jeff Lowery, Jul 23, 2004
    #1
    1. Advertising

  2. 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.
    Christopher T King, Jul 23, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Maurer
    Replies:
    3
    Views:
    4,843
    Peter
    Apr 19, 2006
  2. Wally
    Replies:
    1
    Views:
    2,787
    pvdg42
    Mar 20, 2006
  3. TheDustbustr
    Replies:
    1
    Views:
    450
    Sami Hangaslammi
    Jul 25, 2003
  4. Replies:
    9
    Views:
    539
  5. Chris Withers

    Problems with email.Generator.Generator

    Chris Withers, Sep 11, 2006, in forum: Python
    Replies:
    20
    Views:
    1,693
    Max M
    Sep 12, 2006
Loading...

Share This Page