variable assignment in "while" loop

Discussion in 'Python' started by Sybren Stuvel, Jul 29, 2003.

  1. Hi there,

    Is it possible to use an assignment in a while-loop? I'd like to do
    something like "loop while there is still something to be read, and if
    there is, put it in this variable". I've been a C programmer since I
    was 14, so a construct like:

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

    comes to mind. Unfortunately, this doesn't work. Is there a similar
    construct in python?

    Sybren

    PS: This is a supersede. If it didn't work out and you saw two
    messages with the same content, please ignore the oldest one.
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
     
    Sybren Stuvel, Jul 29, 2003
    #1
    1. Advertising

  2. Sybren Stuvel

    Peter Hansen Guest

    Sybren Stuvel wrote:
    >
    > Is it possible to use an assignment in a while-loop? I'd like to do
    > something like "loop while there is still something to be read, and if
    > there is, put it in this variable". I've been a C programmer since I
    > was 14, so a construct like:
    >
    > while info = mydbcursor.fetchone():
    > print "Information: "+str(info)
    >
    > comes to mind. Unfortunately, this doesn't work. Is there a similar
    > construct in python?


    Yes:

    while 1:
    info = mydbcursor.fetchone()
    if not info:
    break
    print "Information: " + str(info)

    -Peter
     
    Peter Hansen, Jul 29, 2003
    #2
    1. Advertising

  3. Sybren Stuvel

    Aahz Guest

    In article <>,
    Sybren Stuvel <> wrote:
    >
    >Is it possible to use an assignment in a while-loop? I'd like to do
    >something like "loop while there is still something to be read, and if
    >there is, put it in this variable". I've been a C programmer since I
    >was 14, so a construct like:
    >
    >while info = mydbcursor.fetchone():
    > print "Information: "+str(info)
    >
    >comes to mind. Unfortunately, this doesn't work. Is there a similar
    >construct in python?


    Peter usually gives a good answer, but this time there's a better
    answer:

    def fetch_iter(cursor):
    info = True
    while info:
    info = cursor.fetchone()
    yield info

    for info in fetch_iter(mydbcursor):
    print "Information:" + str(info)

    Generally speaking, any time you want to do assignment as part of a
    while loop, you really want to convert it a for loop.

    BTW, DO NOT USE TABS in Python programs. You WILL regret it.
    --
    Aahz () <*> http://www.pythoncraft.com/

    This is Python. We don't care much about theory, except where it intersects
    with useful practice. --Aahz
     
    Aahz, Jul 29, 2003
    #3
  4. "Aahz" <> wrote:
    >
    > Peter usually gives a good answer, but this time there's a better
    > answer:


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

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

    - Anders
     
    Anders J. Munch, Jul 29, 2003
    #4
  5. Skip Montanaro, Jul 29, 2003
    #5
  6. Sybren Stuvel

    Evan Simpson Guest

    Sybren Stuvel wrote:
    > while info = mydbcursor.fetchone():
    > print "Information: "+str(info)


    Gustavo has already pointed out the classic Pythonic ways of writing
    this. In the general case, where you don't have an iterator handed to
    you, you can make one as of Python 2.2 like so:

    def each(f):
    '''Make a zero-argument function or method iterable.
    It had better have side effects, or this is pointless.'''
    v = f()
    while v:
    yield v
    v = f()

    for info in each(mydbcursor.fetchone):
    print "Information:", info

    Of course, all this really does is to factor out one of the classic
    Pythonic patterns into a wrapper function.

    There's also the Pita pocket solution:

    class Pita(object):
    __slots__ = ('pocket',)
    marker = object()
    def __init__(self, v=marker):
    if v is not self.marker:
    self.pocket = v
    def __call__(self, v=marker):
    if v is not self.marker:
    self.pocket = v
    return self.pocket

    p = Pita(10)
    while p(p() - 1):
    print p()

    Cheers,

    Evan @ 4-am
     
    Evan Simpson, Jul 29, 2003
    #6
  7. On 29 Jul 2003 12:08:50 GMT, Sybren Stuvel <> wrote:

    >Hi there,
    >
    >Is it possible to use an assignment in a while-loop? I'd like to do
    >something like "loop while there is still something to be read, and if
    >there is, put it in this variable". I've been a C programmer since I
    >was 14, so a construct like:
    >
    >while info = mydbcursor.fetchone():
    > print "Information: "+str(info)
    >
    >comes to mind. Unfortunately, this doesn't work. Is there a similar
    >construct in python?
    >

    I thought of a little variation on the list comprehension hack that is usually advised against:

    while [info for info in [mydbcursor.fetchone()] if info]:
    print "Information: %s" % info

    I sort of like the mnemonic of the if info that makes the while
    see [] at the end rather than e.g. [None][0]

    Regards,
    Bengt Richter
     
    Bengt Richter, Jul 29, 2003
    #7
    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. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,274
    Tim Slattery
    Mar 30, 2005
  2. Andy Todd
    Replies:
    0
    Views:
    812
    Andy Todd
    Jul 29, 2003
  3. Andy Todd
    Replies:
    1
    Views:
    394
    Raymond Hettinger
    Jul 30, 2003
  4. Uday Bidkar
    Replies:
    4
    Views:
    509
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    419
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page