By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,040 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

keeping Python code properly indented

P: n/a
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'.
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
be*******@aol.com writes:
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
Jul 18 '05 #2

P: n/a
be*******@aol.com writes:
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.

--
ha************@boeing.com
6-6M31 Knowledge Management
Phone: (425) 342-5601
Jul 18 '05 #3

P: n/a

<be*******@aol.com> wrote in message
news:30**************************@posting.google.c om...
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
Jul 18 '05 #4

P: n/a
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'
be*******@aol.com wrote:
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 ~~~~~)
(------------------------------(
Jul 18 '05 #5

P: n/a

<be*******@aol.com> wrote in message
news:30**************************@posting.google.c om...
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.

Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.