Python indentation

Steve Lamb grey at despair.dmiyu.org
Thu Jul 8 12:48:55 EDT 2004


On 2004-07-08, Antoon Pardon <apardon at forel.vub.ac.be> wrote:
> Well personnaly if i have a loop with the terminating condition
> in the middle I now have to write it like this

> while True:
>   code
>   if condition: break
>   code

> I would prefer to indent sucu code as follows:

> while True:
>   code
> if condition: break
>   code

    In both cases you should get a stern talking to in either case.  Why?
Make condition really long.

while 1:
    do(a_thing)
    if (some_really_long_thing > some_other_long_thing): break
    do(something)

   It isn't obvious that the if is doing anything.

while 1:
    do(a_thing)
    if (some_really_long_thing > some_other_long_thing):
        break
    do(something)

   Oh, look, the if does something there.  The idention and block, even though
it is a single-line block, tells us at a glance what is going on.  The code is
better comprehended that way.  Your way, both of them, only serves to confuse.

-- 
         Steve C. Lamb         | I'm your priest, I'm your shrink, I'm your
       PGP Key: 8B6E99C5       | main connection to the switchboard of souls.
-------------------------------+---------------------------------------------



More information about the Python-list mailing list