variable assignment in "while" loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sybren Stuvel

    variable assignment in "while" loop

    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.fetc hone():
    print "Informatio n: "+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?
  • Peter Hansen

    #2
    Re: variable assignment in "while&quo t; loop

    Sybren Stuvel wrote:[color=blue]
    >
    > 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.fetc hone():
    > print "Informatio n: "+str(info)
    >
    > comes to mind. Unfortunately, this doesn't work. Is there a similar
    > construct in python?[/color]

    Yes:

    while 1:
    info = mydbcursor.fetc hone()
    if not info:
    break
    print "Informatio n: " + str(info)

    -Peter

    Comment

    • Aahz

      #3
      Re: variable assignment in "while&quo t; loop

      In article <slrnbicoqq.6gh .sybrenUSE@sybr en.thirdtower.c om>,
      Sybren Stuvel <sybrenUSE@YOUR thirdtower.imag ination.com> wrote:[color=blue]
      >
      >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.fetc hone():
      > print "Informatio n: "+str(info)
      >
      >comes to mind. Unfortunately, this doesn't work. Is there a similar
      >construct in python?[/color]

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

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

      for info in fetch_iter(mydb cursor):
      print "Informatio n:" + 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 (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

      This is Python. We don't care much about theory, except where it intersects
      with useful practice. --Aahz

      Comment

      • Anders J. Munch

        #4
        Re: variable assignment in &quot;while&quo t; loop

        "Aahz" <aahz@pythoncra ft.com> wrote:[color=blue]
        >
        > Peter usually gives a good answer, but this time there's a better
        > answer:[/color]

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

        def fetch_iter(mydb cursor):
        while 1:
        info = mydbcursor.fetc hone()
        if not info:
        break
        yield info

        - Anders


        Comment

        • Skip Montanaro

          #5
          Re: variable assignment in &quot;while&quo t; loop


          Sybren> Is it possible to use an assignment in a while-loop?

          Nope. To learn why, read question 6.30 of the FAQ:



          Skip

          Comment

          • Evan Simpson

            #6
            Re: variable assignment in &quot;while&quo t; loop

            Sybren Stuvel wrote:[color=blue]
            > while info = mydbcursor.fetc hone():
            > print "Informatio n: "+str(info)[/color]

            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 "Informatio n:", 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



            Comment

            • Bengt Richter

              #7
              Re: variable assignment in &quot;while&quo t; loop

              On 29 Jul 2003 12:08:50 GMT, Sybren Stuvel <sybrenUSE@YOUR thirdtower.imag ination.com> wrote:
              [color=blue]
              >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.fetc hone():
              > print "Informatio n: "+str(info)
              >
              >comes to mind. Unfortunately, this doesn't work. Is there a similar
              >construct in python?
              >[/color]
              I thought of a little variation on the list comprehension hack that is usually advised against:

              while [info for info in [mydbcursor.fetc hone()] if info]:
              print "Informatio n: %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

              Comment

              Working...