statements in control structures (Re: Conditional Expressions don't solve the problem)

Christopher A. Craig com-nospam at ccraig.org
Thu Oct 18 14:27:51 CEST 2001


huaiyu at gauss.almadan.ibm.com (Huaiyu Zhu) writes:

> Do you recommend always using this
>     
>     while 1:
>         if not A:
>             E
>             break
>         B
>         if C: break
>         D
>     
> in place of this?
>     
>     while A:
>         B
>         if C: break
>         D
>     else:
>         E
> 
> This style change is orthogonal to the issue of statements before condition.
> The question is whether the else-clause in while-loop has any real use.

I don't, and I don't think he does either.  I do, however, recommend always
using:

     while 1:
         if not A:
             E
             break
         B
         if C: break
         D

in place of

     state = 0
     while 1:
         if not A:
             state = 1
             break
         B
         if C: break
         D
     else:
         if state: E


Which more closely describes your example.  

> To most of the alternatives you mention my immediate reaction is one of
> these two questions:
> 
> 1. Is it practical to change all while loops to for-loops with iterators?
> 2. Is it practical to change all elif into nested scope with break / return?
> 

No it is not practical to do either.  But I would make that change in any
examples that you provided.  

> I'd guess no.  Of course for any simple example it would appear so, and any
> complicated example may appear contrived.  

The problem with your proposal is that all of the examples I saw would be
better written some other way.  One thing you need to get general acceptance
of this PEP is examples in real, in use, code (the standard library would be a
good place to start) where readability would benefit significantly from your
proposal.

I personnally don't think they exist because I think the "problem" with
enclosing statements in conditionals is simply one of trying to impose another
language's style on Python.  You are free to prove me wrong though.

-- 
Christopher A. Craig <com-nospam at ccraig.org>




More information about the Python-list mailing list