while loop with the condition used in the body

U

Ulrich Eckhardt

Hi!

I'm looking for a way to write code similar to this C code:

while(rq = get_request(..)) {
handle_request(rq);
}

Currently I'm doing

while True:
rq = get_request(...)
if not rq:
break
handle_request(rq)

in Python 2.6. Any suggestions how to rewrite that?

Uli
 
A

Arnaud Delobelle

Ulrich said:
Hi!

I'm looking for a way to write code similar to this C code:

while(rq = get_request(..)) {
handle_request(rq);
}

Currently I'm doing

while True:
rq = get_request(...)
if not rq:
break
handle_request(rq)

in Python 2.6. Any suggestions how to rewrite that?

This is the common idiom.
 
P

Peter Otten

Ulrich said:
I'm looking for a way to write code similar to this C code:

while(rq = get_request(..)) {
handle_request(rq);
}

Currently I'm doing

while True:
rq = get_request(...)
if not rq:
break
handle_request(rq)

in Python 2.6. Any suggestions how to rewrite that?

Assuming get_request(...) is called with the same arguments on each
iteration and uses None to signal that there is no more data:

from functools import partial

for rq in iter(partial(get_request, ...), None):
handle_request(rq)

Peter
 
P

Peter Otten

Duncan said:
and the next step on from this is to realise that the problem isn't how to
code the calls to get_request(), the problem is actually that
get_request() itself isn'ty Pythonic. Rewrite it as a generator, rename it
to reflect that it now generates a sequence of requests and the code
becomes:

for rq in incoming_requests(...):
handle_request(rq)

....and a likely implementation would be

def incoming_requests(...):
while True:
rq = ... # inlined version of get_request()
if not rq:
break
yield rq

In other words: It's turtles all the way down...

Peter
 
U

Ulrich Eckhardt

Peter said:
...and a likely implementation would be

def incoming_requests(...):
while True:
rq = ... # inlined version of get_request()
if not rq:
break
yield rq

In other words: It's turtles all the way down...

Almost. While it moves the ugliness, at least it allows separating the
iteration logic from the handling logic, which is already a big step ahead!

That said, there is:

with <expression> as <name>:
...

so why not:

while <expression> as <name>:
...

and also:

if <expression> as <name>:
...


Thanks to everybody for their input!

Uli
 
G

Gregory Ewing

Ulrich said:
so why not:

while <expression> as <name>:
...

and also:

if <expression> as <name>:
...

This sort of thing has been suggested repeatedly in the
past, and it's always been rejected. That's not likely to
change. Look up the past threads for the reasons why.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top