[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

while loop with the condition used in the body

Ulrich Eckhardt

2/24/2010 9:15:00 AM

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

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

6 Answers

Arnaud Delobelle

2/24/2010 10:07:00 AM

0



Ulrich Eckhardt wrote:
> 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.

--
Arnaud

Peter Otten

2/24/2010 10:22:00 AM

0

Ulrich Eckhardt wrote:

> 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

Duncan Booth

2/24/2010 10:32:00 AM

0

Peter Otten <__peter__@web.de> wrote:

> Ulrich Eckhardt wrote:
>
>> I'm looking for a way to write code similar to this C code:
>>
>> while(rq = get_request(..)) {
>> handle_request(rq);
>> }
>>
> 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

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)


--
Duncan Booth http://kupuguy.bl...

Peter Otten

2/24/2010 10:39:00 AM

0

Duncan Booth wrote:

> Peter Otten <__peter__@web.de> wrote:
>
>> Ulrich Eckhardt wrote:
>>
>>> I'm looking for a way to write code similar to this C code:
>>>
>>> while(rq = get_request(..)) {
>>> handle_request(rq);
>>> }
>>>
>> 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
>
> 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

Ulrich Eckhardt

2/24/2010 11:25:00 AM

0

Peter Otten wrote:
> Duncan Booth wrote:
>> 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...

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

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

Gregory Ewing

2/25/2010 7:47:00 AM

0

Ulrich Eckhardt wrote:

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

--
Greg