keeping Python code properly indented

David Brown david at no.westcontrol.spam.com
Fri Jan 16 07:59:30 EST 2004


<beliavsky at aol.com> wrote in message
news:3064b51d.0401151125.711ade4c at posting.google.com...
> How do you keep Python code properly indented as you modify it? I use
> an Emacs-type editor that has a Python mode, so the initial indenting
> is easy. If I later want to put a 'for' loop (or an 'if' statement)
> around a bunch of code, I find myself going through the body of the
> loop, manually re-indenting to keep the nested loops correct. There
> should be a better way.
>

Any decent editor will do the indenting in a couple of keystrokes.  And even
doing it manually, if your looped code is so many lines that manually
"tabbing" each is a a big chore, you should probably break it into smaller
functions for clarity.

> I think that having a 'for' loop end with a matching 'next' statement,
> as done in (for example) Basic, is a safer and clearer way of writing
> loops, although some Python programmers consider this a 'feature'
> rather than a 'bug'.

When programming in Basic, you still have to indent the contents of the loop
(manually or otherwise, depending on the editor).  Just because the basic
interpreter / compiler will work without sensible indentation, does not mean
that mismatches between the code structure and the code appearance
(especially indentation) is in any way a good thing.






More information about the Python-list mailing list