Assignment in a while?
Fredrik Lundh
fredrik at pythonware.com
Sun Apr 2 15:24:03 EDT 2006
"none <"@bag.python.org> wrote:
> So it seems that I stumbled on the idiomatic way of doing this then.
> Well, as they say, "When in Rome..." :). Thanks for pointing out the
> FAQ. I'll be reading up on it.
the idiomatic way to loop in Python is to use iterators/generators. if
you have a callable that fetches data from some resource and returns
a "sentinel" when you get to the end, you can use the iter function to
turn it into an iterator:
>>> help(iter)
Help on built-in function iter in module __builtin__:
iter(...)
iter(collection) -> iterator
iter(callable, sentinel) -> iterator
Get an iterator from an object. In the first form, the argument must
supply its own iterator, or be a sequence.
In the second form, the callable is called until it returns the sentinel.
given this, your loop can be written:
for result in iter(std.fetchone, None):
print result
</F>
More information about the Python-list
mailing list