Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Creating an iterator in a class (http://www.velocityreviews.com/forums/t955861-creating-an-iterator-in-a-class.html)

Joseph L. Casale 12-27-2012 02:44 PM

Creating an iterator in a class
 
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

TommyVee 12-27-2012 03:34 PM

Re: Creating an iterator in a class
 
"Joseph L. Casale" wrote in message
news:mailman.1346.1356619576.29569.python-list@python.org...

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.



All times are GMT. The time now is 02:57 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.