keeping Python code properly indented

H

Harry George

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.

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'.

You do not need to "go through the body, manually re-indenting". In
emacs, block select everything to be indented and then do the right
shift in one step. That action is in the python-mode.el's added menu,
and it is available as keystrokes ("C-c >" by default). Similarly for
undenting.

Is there a safety concern which requires solution through closure
markers? It is a potential problem, but isn't worth closure markers
in the language. The main solution is to not let indenting get out of
whack in the first place:

a) Have a rigorous newbie setup process to assure the editor does
4-char hard spaces (no tab chars), and understands python-mode
indenting. See: http://www.python.org/peps/pep-0008.html

b) During development, run unittest-based regression tests every
minute or so. A messed-up indent will either fail to compile or will
fail a test. This enforces the indents and that becomes valued a
language feature when doing code reviews.

c) Immediately fix indents if problems are detected.

d) Keep the code under revision control, so older (known good) code
can be recovered.

(If you can't assure a-d, then there are bigger problems than language
features.)

Finally, if you really think you need closure markers,
do so with comments instead of language changes:

if x > y:
z=0
#end if

You will still be forced to get the indenting right, but at least you
will have clues for recovering mangled indenting.
 
B

beliavsky

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.

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'.
 
T

Thomas Heller

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.

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'.

In Xemacs with python-mode, I just mark the region and hit 'C-c >'
py-shift-region-right or 'C-c <' py-shift-region-left.

Thomas
 
J

John Roth

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.

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'.

Just about every editor in existence allows you to select a range
of lines and indent or dedent them as a group. If your editor
doesn't let you do this, get a better editor.

John Roth
 
J

Jason Mobarak

The tabnanny module/program is a very good way to make sure python files
are properly indented.

@ares shtoom --> python -c "import tabnanny; tabnanny.check('.')"
../ui/base.py 9 '\tfrom shtoom import prefs \n'
../test/test_stun.py 18 "\tn1 = NetAddress('10/8')\n"
../audio/__init__.py 21 '\taudio = attempt()\n'
../multicast/unixspec.py 24 '\tgroup = gethostbyname(addr)\n'
../multicast/SAP.py 8 '\tself.dismantlePacket(pack)\n'
../multicast/SDP.py 35 "\tlines = text.split('\\n')\n"
../multicast/netnum.py 10 '\tl = l+[0]*(4-len(l))\n'
../rtp.py 128 '\treturn d\n'


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.

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'.

--
(------------------------------(
)~~~~~ Jason A. Mobarak ~~~~~~~)
(~~ aether_at_gentoo_dot_org ~~(
)~~~~ jmob_at_unm_dot_edu ~~~~~)
(------------------------------(
 
D

David Brown

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top