Re: variable assignment in "while" loop

Discussion in 'Python' started by Andy Todd, Jul 29, 2003.

  1. Andy Todd

    Andy Todd Guest

    >>From: Sybren Stuvel [mailto:]
    >>Sent: Martes, 29 de Julio de 2003 07:09 a.m.
    >>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?

    > Well, you say you've programmed C since 14, but not how old you
    > are now, so... you could be programming C for six months if you're
    > 14-and-a-half ;-)
    > Assignment, in Python, is not an expression, it's a statement;
    > it doesn't "return" any value, it binds an object's reference
    > to a name. Multiple chained assignments in Python don't work
    > the same way as they do in C, because in Python they're merely
    > syntactic sugar so you don't have to type them by hand.
    > Anyhow... the best way to do what you wanna is simply:
    > while 1:
    > info = mydbcursor.fetchone()
    > if not info: break
    > print "Information:", info
    > a-ha! you say; yes, the "print" statement ("STATEMENT", not function)
    > automagically applies str() to the given object, so if you directly
    > print an object, you don't have to str() it, print does that for
    > you.
    > "But it's so cumbersome" you think, looking upon an inconditional-
    > turned-conditional loop. Don't worry, it'll become natural with
    > practice, and after a bit you'll recall your previous C loops
    > and think "ewww". Why? Because, the C compiler is doing
    > exactly that, only implicitly, and hiding it from you.
    > That's bad.
    > You could also:
    > info = mydbcursor.fetchone()
    > while info:
    > print "Information:", info
    > info = mydbcursor.fetchone()
    > and there's nothing wrong with this; the only "anti-aesthetic"
    > thing here is the duplicated line which assigns to "info",
    > but that's small potatos, really; don't worry about that.
    > Another way, with more modern versions of Python, is to
    > iterate directly over the cursor:
    > for info in mydbcursor:
    > print "Information:", info
    > Why? Because cursors, if I recall correctly, are iterable
    > objects, so you can iterate through them using for: or any
    > other similar construct.
    > Welcome to programming bliss :)
    > -gustavo

    Spot on, with one (minor) correction. With a for loop you have to
    iterate over the results of a call to a method on the cursor, e.g.;

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

    You could replace fetchall() with fetchmany() or, if you are feeling
    contrary, fetchone() ;-)

    From the desk of Andrew J Todd esq -
    Andy Todd, Jul 29, 2003
    1. Advertisements

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
    Tim Slattery
    Mar 30, 2005
  2. Sybren Stuvel

    variable assignment in "while" loop

    Sybren Stuvel, Jul 29, 2003, in forum: Python
    Bengt Richter
    Jul 29, 2003
  3. Andy Todd
    Raymond Hettinger
    Jul 30, 2003
  4. Uday Bidkar
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  5. Isaac Won
    Ulrich Eckhardt
    Mar 4, 2013

Share This Page