Re: while (assignment):

Discussion in 'Python' started by John Roth, Jul 30, 2003.

  1. John Roth

    John Roth Guest

    "Sybren Stuvel" <> wrote in message
    news:...
    > 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?


    For most purposes, there is. Just use a for loop!
    As long as the source behaves like an iterator, the for
    loop will automatically assign each result.

    Something like this:

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

    In this, mydbcursor.resultset has to look like
    a sequence of some kind (list or tuple),
    or be an iterator. Since what you presumably
    have is a result set from a data base query,
    that's a rather natural fit. You might have to wrap
    the result set to make it an iterator, but I'd think
    any good db package would catch up eventually.

    HTH
    John Roth
    >
    > Sybren
    > --
    > 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?
     
    John Roth, Jul 30, 2003
    #1
    1. Advertising

  2. John Roth

    Aahz Guest

    In article <>,
    John Roth <> wrote:
    >
    >Something like this:
    >
    > for info in mydbcursor.resultset:
    > print "Information: " + str(info)
    >
    >In this, mydbcursor.resultset has to look like a sequence of some kind
    >(list or tuple), or be an iterator.


    Nitpick: mydbcursor.resultset can be *either* an iterator *or* an
    iterable (an object whose __iter__() method returns an iterator object).
    --
    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 30, 2003
    #2
    1. Advertising

  3. John Roth enlightened us with:
    > For most purposes, there is. Just use a for loop! As long as the
    > source behaves like an iterator, the for loop will automatically
    > assign each result.


    How's that for performance? I guess I'll have to take a look at the
    MySQLdb package source code to see how the resultset code functions. I
    hope it calls fetchone() on each call to next() - AFAIK that's more
    efficient (let the DB ponder what the next row will be while my
    program ponders what to do with the information, and then exchange the
    new tuple)

    Sybren
    --
    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 30, 2003
    #3
    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. Sybren Stuvel

    variable assignment in "while" loop

    Sybren Stuvel, Jul 29, 2003, in forum: Python
    Replies:
    6
    Views:
    421
    Bengt Richter
    Jul 29, 2003
  2. Andy Todd
    Replies:
    0
    Views:
    800
    Andy Todd
    Jul 29, 2003
  3. Andy Todd
    Replies:
    1
    Views:
    388
    Raymond Hettinger
    Jul 30, 2003
  4. nagy
    Replies:
    36
    Views:
    1,011
    Terry Reedy
    Jul 20, 2006
  5. Chris
    Replies:
    34
    Views:
    1,525
Loading...

Share This Page