RE: Creating an iterator in a class

Discussion in 'Python' started by Joseph L. Casale, Dec 27, 2012.

  1. > It's probably best if you use separate cursors anyway. Say you have
    > two methods with a shared cursor:
    >
    > def iter_table_a(self):
    > self.cursor.execute("SELECT * FROM TABLE_A")
    > yield from self.cursor
    >
    > def iter_table_b(self):
    > self.cursor.execute("SELECT * FROM TABLE_B")
    > yield from self.cursor
    >
    > If the client code calls iter_table_a and partially iterates over the
    > result, and then needs to use iter_table_b for some reason, then when
    > it gets back to the table A results the data will be gone. You can
    > only safely share cursors when you can guarantee that each query will
    > be completely processed before the next query is run, and you can't
    > guarantee that if you're just returning iterators.


    Oops, I admit I never tested that scenario before and you are right, its
    not a sane assumption.

    Thanks, I'll rewrite that section.
    jlc
     
    Joseph L. Casale, Dec 27, 2012
    #1
    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. Hendrik Maryns
    Replies:
    18
    Views:
    1,466
  2. greg
    Replies:
    6
    Views:
    480
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    693
    Jim Langston
    Oct 30, 2005
  4. Steven D'Aprano

    What makes an iterator an iterator?

    Steven D'Aprano, Apr 18, 2007, in forum: Python
    Replies:
    28
    Views:
    1,267
    Steven D'Aprano
    Apr 20, 2007
  5. David Bilsby
    Replies:
    5
    Views:
    2,092
    David Bilsby
    Oct 9, 2007
Loading...

Share This Page