variable assignment in "while" loop

Isaac To kkto at csis.hku.hk
Wed Jul 30 01:04:05 EDT 2003


>>>>> "Anders" == Anders J Munch <andersjm at dancontrol.dk> writes:

    Anders> "Aahz" <aahz at pythoncraft.com> 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):
            return
        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)

Regards,
Isaac.




More information about the Python-list mailing list