Re: [Python-ideas] if condition: break idiom

On 21 Sep 2008, at 20:24, Josiah Carlson wrote: [...]
Well my view was that break and continue are the only two statements that relate to loops. [...]
There is a missing link in your interpretation of my argumentation. It is that I haved noticed that, as I do not have a do .. while construct at my disposal in Python, I do not try to shape my loops into this structure anymore. I almost *never* write: while True: ... if condition: break But most of the time it seems that the correct structure for a loop comes as while True: ... if condition: break ... In fact, I would be happy with getting rid of the while loop and replacing it with a simple 'loop' constuct, where: while condition: ... would be written as: loop: if condition: break ... However I see this as too radical to propose :)
This makes a lot of sense, if you use a do .. while concept in your loops. Now I feel bad that I used the word "uglier" ...
Thank you for your comments, they all make a lot of sense. -- Arnaud

On Sun, Sep 21, 2008 at 12:52 PM, Arnaud Delobelle <arnodel@googlemail.com> wrote:
Superficially yes, but the execution of raise, return or yield can result in the loop never completing another pass. That a loop can occur in an except clause even offers the ability for the no-argument raise to make sense even there ;) .
That also has a translation with the "first" idiom, though it's a bit uglier (no, really): first = 1 while first or condition: if not first: ... first = 0 ... Really though, what is pretty/elegant really varies depending on what those ... turn into. To be honest, I've only ever used the above once or twice, though the standard first idiom I've used dozens of times. Though if you really want to have fun, there's nothing like exploiting while loops for checking error conditions. ;) def foo(...): first = 1 while first: first = 0 if error_condition1: break ... #other error conditions, processing, whatever else: #only executes if everything is ok #error condition - Josiah

On 21 Sep 2008, at 21:24, Josiah Carlson wrote: [...]
Why not write: def foo(...): while True: if error_condition1: break # Other error conditions, processing, etc # only executes if everything is OK # Error condition Is there something that I don't see? -- Arnaud

On Sun, Sep 21, 2008 at 12:52 PM, Arnaud Delobelle <arnodel@googlemail.com> wrote:
Superficially yes, but the execution of raise, return or yield can result in the loop never completing another pass. That a loop can occur in an except clause even offers the ability for the no-argument raise to make sense even there ;) .
That also has a translation with the "first" idiom, though it's a bit uglier (no, really): first = 1 while first or condition: if not first: ... first = 0 ... Really though, what is pretty/elegant really varies depending on what those ... turn into. To be honest, I've only ever used the above once or twice, though the standard first idiom I've used dozens of times. Though if you really want to have fun, there's nothing like exploiting while loops for checking error conditions. ;) def foo(...): first = 1 while first: first = 0 if error_condition1: break ... #other error conditions, processing, whatever else: #only executes if everything is ok #error condition - Josiah

On 21 Sep 2008, at 21:24, Josiah Carlson wrote: [...]
Why not write: def foo(...): while True: if error_condition1: break # Other error conditions, processing, etc # only executes if everything is OK # Error condition Is there something that I don't see? -- Arnaud
participants (2)
-
Arnaud Delobelle
-
Josiah Carlson