473,657 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

keeping Python code properly indented

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
5 2596
be*******@aol.c om 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
be*******@aol.c om 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

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

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
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(a ddr)\n'
../multicast/SAP.py 8 '\tself.dismant lePacket(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.c om 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_gento o_dot_org ~~(
)~~~~ jmob_at_unm_dot _edu ~~~~~)
(------------------------------(
Jul 18 '05 #5

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

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
33863
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
147
7699
by: Sateesh | last post by:
Hi, I am a beginner in Python, and am wondering what is it about the indentation in Python, without which python scripts do not work properly. Why can't the indentation not so strict so as to give better freedom to the user? Is there any plausible reason behind this? Cheers! Sateesh
11
1755
by: Alex Martelli | last post by:
Thanks to David Bolen, who did the build, I have been able to make available a win32 packaging for gmpy 1.0 on python 2.4 alpha 2 (should work on any later Python 2.4 as well, but I have no way to check it at all). I can't reach sourceforge at the moment, so I have meanwhile put the exe file up at: http://www.aleax.it/Python/gmpy-1.0.win32-py2.4.exe Users of both gmpy and python 2.4 on windows are invited to dowload and install this,...
3
2928
by: srijit | last post by:
Hello, Here is an example code of xml writer in Python+PythonNet, Ironpython and Boo. The codes look very similar. Regards, Srijit Python + PythonNet: import CLR
852
28325
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for my general education. Mark
3
2427
by: Paddy3118 | last post by:
Not python: but python type indented text Notice the blank line above. It could have several spaces or tabs, and still be a part of the block beginning 'Not python:': The block ends at the
20
1392
by: Eric Wertman | last post by:
I was considering putting together a proposal for an alternate block syntax for python, and I figured I'd post it here and see what the general reactions are. I did some searching, and while I found a lot of tab vs space debates, I didn't see anything like what I'm thinking of, so forgive me if this is a very dead horse. Generally speaking, I like the current block scheme just fine. I use python on a daily basis for system...
5
1349
by: fishfin | last post by:
I was working through a tutorial about how to write a server using python (the url is bellow). I am sure that the server is working to some degree because when the server is running localhost:8080 just keeps trying to load until it times out. I would like to know how to send information through the server to my browser? I was working through this tutorial: http://python.about.com/od/networkingwithpython/ss/PythonWebServer.htm
0
8385
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8303
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8821
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8502
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8602
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1941
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.