Creating an iterator in a class

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

  1. I am writing a class to provide a db backed configuration for an application.

    In my programs code, I import the class and pass the ODBC params to the
    class for its __init__ to instantiate a connection.

    I would like to create a function to generically access a table and providean
    iterator. How does one create a function in a class that takes an argument and
    returns an iterator? I saw some examples where the class gets instantiated
    with the table defined but I was hoping not to do this so I could continue to
    access various tables through one connection/cursor.

    Thanks!
    jlc
    Joseph L. Casale, Dec 27, 2012
    #1
    1. Advertising

  2. Joseph L. Casale

    TommyVee Guest

    "Joseph L. Casale" wrote in message
    news:...

    I am writing a class to provide a db backed configuration for an
    application.

    In my programs code, I import the class and pass the ODBC params to the
    class for its __init__ to instantiate a connection.

    I would like to create a function to generically access a table and provide
    an
    iterator. How does one create a function in a class that takes an argument
    and
    returns an iterator? I saw some examples where the class gets instantiated
    with the table defined but I was hoping not to do this so I could continue
    to
    access various tables through one connection/cursor.

    Thanks!
    jlc=

    Python makes it easy. Read up on "Generators", and the "yield" statement.
    Works very elegantly with a "for ... in" loop.
    TommyVee, Dec 27, 2012
    #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. Hendrik Maryns
    Replies:
    18
    Views:
    1,419
  2. greg
    Replies:
    6
    Views:
    454
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    643
    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,156
    Steven D'Aprano
    Apr 20, 2007
  5. David Bilsby
    Replies:
    5
    Views:
    2,041
    David Bilsby
    Oct 9, 2007
Loading...

Share This Page