variable assignment in "while" loop

Isaac To kkto at
Wed Jul 30 07:04:05 CEST 2003

>>>>> "Anders" == Anders J Munch <andersjm at> writes:

    Anders> "Aahz" <aahz at> wrote:
    >>  Peter usually gives a good answer, but this time there's a better
    >> answer:

    Anders> With a bug, but if we combine Peter's answer with yours we get
    Anders> an even better answer:

    Anders> def fetch_iter(mydbcursor): while 1: info =
    Anders> mydbcursor.fetchone() if not info: break yield info

Or perhaps use a function instead...

def while_iter(f):
    while True:
        result = f()
        if (not result):
        yield result

Then you can convert

    while info = mydbcursor.fetchone():
        print "Information: "+str(info)

trivially to

    for info in while_iter(lambda: mydbcursor.fetchone()):
        print "Information: "+str(info)


More information about the Python-list mailing list