variable assignment in "while" loop

S

Sybren Stuvel

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.
 
P

Peter Hansen

Sybren said:
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
 
A

Aahz

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.
 
A

Anders J. Munch

Aahz said:
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
 
E

Evan Simpson

Sybren said:
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
 
B

Bengt Richter

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top