[Tutor] Just a Python formatting question

Alan G alan.gauld at freenet.co.uk
Fri Jun 10 09:31:34 CEST 2005


> And all the lines that belong to the statement also. It does not
matter if
> is a nested sequence. Let me lighten you:
>
> for i in somelist:
>     do_something
>     do_something_else
>     if x==y:
>         inside the if clause
>         all statements go
>         with 4 spaces after the if line
>     elif x>y:
>         the same here

While this is good advice Python doesn't mind if the elif uses
different
indentation to the if. Indeed each elif block could be different, so
long
as its consistent within the block. But that could lead to very wierd
code:

if something:
           do a thing
           and this
elif another :
 do another thing
 and that
elif what:
        something here
        and here
else: pass


Now visually that's a mess but Python won't complain because each
block
is consistent. So to please Python be consistent inside a block, but
to
make it readable be consistent across all blocks!

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list